Provides a set of methods for working with forms and especially forms related to objects assigned to the template. The following is an example of a complete form for a person object that works for both creates and updates built with all the form helpers. The @person object was assigned by an action on the controller:

  <form action="save_person" method="post">
    Name:
    <%= text_field "person", "name", "size" => 20 %>

    Password:
    <%= password_field "person", "password", "maxsize" => 20 %>

    Single?:
    <%= check_box "person", "single" %>

    Description:
    <%= text_area "person", "description", "cols" => 20 %>

    <input type="submit" value="Save">
  </form>

…is compiled to:

  <form action="save_person" method="post">
    Name:
    <input type="text" id="person_name" name="person[name]"
      size="20" value="<%= @person.name %>" />

    Password:
    <input type="password" id="person_password" name="person[password]"
      size="20" maxsize="20" value="<%= @person.password %>" />

    Single?:
    <input type="checkbox" id="person_single" name="person[single]" value="1" />

    Description:
    <textarea cols="20" rows="40" id="person_description" name="person[description]">
      <%= @person.description %>
    </textarea>

    <input type="submit" value="Save">
  </form>

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

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

…becomes:

  <input type="text" id="person_<%= @person.id %>_name" name="person[<%= @person.id %>][name]" 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 type="text" id="person_1_name" name="person[1][name]" value="<%= @person.name %>" />

There’s also methods for helping to build form tags in ActionView::Helpers::FormOptionsHelper ActionView::Helpers::DateHelper and ActionView::Helpers::ActiveRecordHelper

Show files where this module is defined (1 file)
Register or log in to add new notes.