Validations with form_for
Last updated
Last updated
form_for
Now that we know Rails automatically performs validations defined on models, let's use this information to easily display validation errors to the user.
After this lesson, you'll be able to...
use form_for
to display a form with Validations
print out full error messages above the form
form_for
and form_tag
This step will make heavy usage of form_for
, the high-powered alternative to form_tag
. The biggest difference between these two helpers is that form_for
creates a form specifically for a model object. form_for
is full of convenient features.
In the example below, @post
is the model object that needs a form. form_for
automatically performs a route lookup to find the right URL for post.
form_for
takes a block. It passes an instance of as a parameter to the block, which is what f
is below.
A basic implementation looks like this:
This creates the HTML:
Here's what we would need to do with form_tag
to generate the exact same HTML:
form_tag
doesn't know what action we're going to use it for, because it has no model object to check. form_for
knows that an empty, unsaved model object needs a new
form and a populated object needs an edit
form. This means we get to skip all of these steps:
Setting the name
and id
of the <form>
element.
Setting the method to patch
on edits.
Setting the text of the <submit>
element.
Specifying the root parameter name (post[whatever]
) for every field.
Choosing the attribute (@post.whatever
) to fill for every field.
Nifty!
form_for
to generate empty formsTo wire up an empty form in our new
view, we need to create a blank object:
Here's our usual vanilla create
action:
We still have to solve the dual problem of what to do when there's no valid model object to redirect to, and how to hold on to our error messages while re-rendering the same form.
Remember from a few lessons ago how CRUD methods return false
when validation fails? We can use that to our advantage here and branch our actions based on the result:
Because of form_for
, Rails will automatically prepopulate the new
form with the values the user entered on the previous page.
To get some extra verbosity, we can add the snippet from the previous lesson to the top of the form:
field_with_errors
Let's look at another nice feature of FormBuilder
. Here's our form_for
code again:
The text_field
call generates this tag:
Not only will FormBuilder
pre-fill an existing Post
object's data, it will also wrap the tag in a div
with an error class if the field has failed validation(s):
This can also result in some unexpected styling changes because <div>
is a block tag (which takes up the entire width of its container) while <input>
is an inline tag. If your layout suddenly gets messed up when a field has errors, this is probably why.
form_for
gives us a lot of power!
Our challenge as developers is to keep track of the different layers of magic that make this tool so convenient. The old adage is true: we're responsible for understanding not only how to use form_for
but also why it works. Otherwise, we'll be completely lost as soon as a sufficiently unusual edge case appears.
When in doubt, read the HTML. Get used to hitting the "View Source" and "Open Inspector" hotkeys in your browser (Ctrl-u
and Ctrl-Shift-i
on Chrome Windows; Option-Command-u
and Option-Command-i
on Chrome Mac), and remember that most browsers let you .
View on Learn.co and start learning to code for free.
View on Learn.co and start learning to code for free.