Rails Forms Overview
Imagine that you're on a roadtrip across the country (I'm already jealous) with a starting point of Santa Barbara and a final destination of New York City. If you enter the addresses into Google Maps, you'll be shown multiple routes, and each route has an associated duration.
How do you select which route to take? Some of the points that should be included in your decision are below:
Duration
Cities that you go through
Landmarks that you want to drive through
Forms in Rails are similar to your roadtrip. Rails supplies a few different options to choose from when creating forms. How do you know the right option to select? Just like your roadtrip you consider the strengths of each form option and see how well it aligns with your intended behavior and the application requirements.
In this lesson we will review:
Both of the main form implementations in Rails
Discuss when one type of form should be selected over the other
Walk through the different form options
form_tag
form_tag
Attributes of the form_tag
helper:
Most basic form helper that's available in Rails
Uses tag form elements to build out a form
Unlike the
form_for
helper, it does not use a form builder
Let's build out a form that lets users enter in their cat's name and their associated color:
This will build a form and auto generate the following HTML:
form_for
form_for
Attributes for the form_for
form helper method:
More magical form helper in Rails
form_for
is a ruby method into which a Ruby object is passed. This means that a form that utilizesform_for
is directly connected with an Active Record modelform_for
yields aFormBuilder
object that lets you create form elements that correspond to attributes in the model
So, what does this all mean? When you're using the form_for
method, the object is passed as a form_for
parameter, and it creates corresponding inputs with each of the attributes. For example, if you have form_for(@cat)
, the form field params would look like cat[name]
, cat[color]
, etc
Let's refactor the cat form that we discussed in the previous section. With form_for
, it can be simplified to look like this:
The form_for
above will auto generate the following HTML:
Differences between form_for
and form_tag
form_for
and form_tag
Getting back to our roadtrip example, in order to make an informed decision on what route to take we need to know everything possible about both options. In like manner, in order to make a good choice for which form element to use, it's vital to understand the subtle but extremely important differences between the two:
form_for
is essentially an advanced form helper that will yield aFormBuilder
object that you use to generate your form elements (text fields, labels, a submit button, etc.)form_tag
is a lower-level form helper that simply generates aform
element. To add fields to theform_tag
block, you add form element tags, such astext_field_tag
,number_field_tag
,submit_tag
, etc.form_tag
makes no assumptions about what you're trying to do, and you're responsible for specifying exactly what the form is supposed to do (send aPOST
request,PATCH
request, etc.)form_for
handles the retrieval of values from your object model and will also try to route the form to the appropriate action specified in the controller
So, when would you choose one over the other? Below are some real world examples:
form_for
- this works well for forms that manage CRUD. Imagine that you have a blog posting application. A great fit for theform_for
method would be thePost
model. This is because thePost
model would have the standard Active Record setup, and therefore it's smart to take advantage of the prepackaged functionality built intoform_for
.form_tag
- this works well for forms that are not directly connected with models. For example, let's say that our blog posting application has a search engine. The search form would be a great fit for using aform_tag
.
Last updated