Topic: Introduction to Form Helpers

Form helpers are methods to aid in the creation of HTML forms. For example, this will create a text field with the name 'foo':

<%= text_field_tag :foo %>

Whatever the user types into this text field can be accessed in the next controller action using params[:foo].

Regular vs. Model Form Helpers

There are two types of form helpers. I'm calling these "regular" and "model". You are probably mostly familiar with the model form helpers which scaffolding generates. An example of a model form helper is this:

<%= text_field :person, :name %>

This helper is used to edit the name attribute inside the @person model. The value will be accessible in the controller using params[:person][:name]. But, take a look at the first code example I gave. Notice the extra "_tag" at the end of the method name? Although it might seem like the same thing, "text_field" and "text_field_tag" are two very different methods.

The regular form helpers usually end in "_tag" and aren't related to models in any way. They can be used for anything.

The model form helpers, on the other hand, are used for editing a model's attributes. In the person code example I gave above, it will look for an instance variable called @person. If is set, it will automatically set the text field to this value. If you were using text_field_tag, you would need to specify the value manually.

When not to use model helpers

Let's say you are adding a search to your site. Don't do this:

<%= text_field :search, :query %>

Most likely there isn't a model called Search, nor does it have an attribute called "query", so you shouldn't use this type of form helper. Instead, use the normal text_field_tag helper like this:

<%= text_field_tag :query %>


Now you know when to use the regular form helpers vs. the model specific form helpers. In this article I only used the text_field helper as an example, but the same rule applies to the other form helpers as well (check_box, radio, select, etc.). There are only a few exceptions (such as date_select vs. select_date, that one always gets me).

Railscasts - Free Ruby on Rails Screencasts

Re: Introduction to Form Helpers

Sweet--thanks for this tutorial. more tutorials which cover some more clarification on the basics like this help me a lot.  I've found there arent much basic tutorials like this which specifically define each function and its inherited components. thanks!

Re: Introduction to Form Helpers

Thanks for this! I've been trying for ages to figure out what I was doing wrong, I was using text_field_tag and expecting it to work like text_field.

// noob mistake smile

Re: Introduction to Form Helpers

Thanks for the nice tutorial, there are so many tutorials out there that it's easy to get overwhelmed and your short precise tutorial on this tag makes it easy to digest.

I have a table called leads with the following columns: firstname, lastname, email, phone, and company_id

company_id refers to a belongs_to and has_many relationship in a table called companies. The companies table has only one column, name.

I'm having difficulty using the <%= text_field :lead =%>

I'm a newbie, why does this not work?

Re: Introduction to Form Helpers

how do i create a set of text fields referring to the same model?I want all of these field value to be sent as an array.

Re: Introduction to Form Helpers

I have just written a useful printable PDF cheatsheet for Rails' form helpers - Making business beautiful.