Chapter 6. Presenting Models with Forms
In many cases, the user interface to a program is the most important part for a commercial company: whether the programs works correctly or not seems to be secondary.
Linus Torvalds
The previous chapter showed how Rails makes it easy to create simple applications using scaffolding, but a key aspect of Rails scaffolding is that it isn’t meant to be permanent. This doesn’t necessarily mean that you’ll tear it down completely and start over, but it usually means that you’ll at least make substantial improvements to make it more attractive. This is especially important where information is coming in from users. While Rails scaffolding provides basic functionality, you’re very likely going to want to improve on the forms it creates.
More Than a Name on a Form
To demonstrate a reasonably complete set of HTML form features, the application needs to support more than one data field and needs to support fields in a variety of types. Rails, because it works with a wide variety of databases, supports a narrower set of types than each of those databases. The types of fields that Rails supports through ActiveRecord include:
:string :text :integer :float :decimal :datetime :timestamp :time :date :binary :boolean
The :string
type is generally limited to 255 characters, whereas :text
can hold longer data. The :integer
, :float
, and :decimal
types all hold numbers, although integers may not have a fractional part to the right of the decimal point. The ...
Get Learning Rails 5 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.