Module

FormHelper

Form helpers are designed to make working with models much easier compared to using just standard HTML elements by providing a set of methods for creating forms based on your models. This helper generates the HTML for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form is submitted (i.e., when the user hits the submit button or form.submit is called via JavaScript), the form inputs will be bundled into the params object and passed back to the controller.

There are two types of form helpers: those that specifically work with model attributes and those that don’t. This helper deals with those that work with model attributes; to see an example of form helpers that don’t work with model attributes, check the ActionView::Helpers::FormTagHelper documentation.

The core method of this helper, form_for, gives you the ability to create a form for a model instance; for example, let’s say that you have a model Person and want to create a new instance of it:

# Note: a @person variable will have been created in the controller.
# For example: @person = Person.new
<% form_for :person, @person, :url => { :action => "create" } do |f| %>
  <%= f.text_field :first_name %>
  <%= f.text_field :last_name %>
  <%= submit_tag 'Create' %>
<% end %>

The HTML generated for this would be:

<form action="/persons/create" method="post">
  <input name="person[first_name]" size="30" type="text" />
  <input name="person[last_name]" size="30" type="text" />
  <input name="commit" type="submit" value="Create" />
</form>

The params object created when this form is submitted would look like:

{"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}

The params hash has a nested person value, which can therefore be accessed with params[:person] in the controller. If were editing/updating an instance (e.g., Person.find(1) rather than Person.new in the controller), the objects attribute values are filled into the form (e.g., the person_first_name field would have that person’s first name in it).

If the object name contains square brackets the id for the object will be inserted. For example:

<%= text_field "person[]", "name" %>

…will generate the following ERb.

<input name="person[<%= @person.id %>][name]" type="text" value="<%= @person.name %>" />

If the helper is being used to generate a repetitive sequence of similar form elements, for example in a partial used by render_collection_of_partials, the index option may come in handy. Example:

<%= text_field "person", "name", "index" => 1 %>

…becomes…

<input name="person[1][name]" type="text" value="<%= @person.name %>" />

There are also methods for helping to build form tags in link:classes/ActionView/Helpers/FormOptionsHelper...., link:classes/ActionView/Helpers/DateHelper.html, and link:classes/ActionView/Helpers/ActiveRecordHelper...

Public Methods
apply_form_for_options!
check_box Returns a checkbox tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). It’s intended that method returns an integer and if that integer is above zero, then the checkbox is checked. Additional options on the input tag can be passed as a hash with options. The checked_value defaults to 1 while the default unchecked_value is set to 0 which is convenient for boolean values. Since HTTP standards say that unchecked checkboxes don’t post anything, we add a hidden value with the same name as the checkbox as a work around.
fields_for Creates a scope around a specific model object like form_for, but doesn’t create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form:
file_field Returns an file upload input tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.
form_for Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
hidden_field Returns a hidden input tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.
label Returns a label tag tailored for labelling an input field for a specified attribute (identified by method) on an object assigned to the template (identified by object). The text of label will default to the attribute name unless you specify it explicitly. Additional options on the label tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.
password_field Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.
radio_button Returns a radio button tag for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). If the current value of method is tag_value the radio button will be checked. Additional options on the input tag can be passed as a hash with options.
text_area Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options.
text_field Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.