FormHelper, FormTagHelper, and FormOptionsHelper

These three classes of helper methods offer different approaches to building forms and some different pieces for creating forms. Much of the time you’ll want to use either FormHelper or FormTagHelper, but you might mix FormOptionsHelper with either of the other two.

The FormHelper methods create form fields bound to particular attributes of ActiveRecord objects. They are easiest to use within a form_for method that sets their context, as described in Chapter 6. If you don’t like that approach, you can supply an object and attribute name as the first two parameters when calling them. (The documentation for each method shows the parameter approach.)

check_box

Creates a checkbox field bound to an attribute from the object specified in form_for or in the parameters. (It also creates a hidden field bound to the same attribute for use if the checkbox isn’t checked.)

fields_for

fields_for is like form_for, except that it doesn’t create the actual form tags.

file_field

Creates a file upload field bound to an attribute from the object specified in form_for or in the parameters. (You’ll need to modify the form_for call as described in Chapter 8 to use this method.)

form_for

Creates a form element and sets the context for the other helper methods in FormHelper.

hidden_field

Creates a hidden field bound to an attribute from the object specified in form_for or in the parameters.

label

Creates a label for a field created with the other methods of FormHelper.

password_field

Creates a password field bound to an attribute from the object specified in form_for or in the parameters.

radio_button

Creates a radio button bound to an attribute from the object specified in form_for or in the parameters.

text_area

Creates a larger multiline text area bound to an attribute from the object specified in form_for or in the parameters.

text_field

Creates a single-line text field bound to an attribute from the object specified in form_for or in the parameters.

The FormTagHelper class does similar work but provides no automatic binding to a single shared object for a form. It lets you build forms where each component is specified separately:

check_box_tag

Lets you create checkboxes. Unlike check_box, it doesn’t automatically create a hidden field for use if the box is unchecked.

field_set_tag

Creates a fieldset tag for grouping form elements. You can set the legend for the fieldset as an argument.

file_field_tag

Creates a file upload tag. To use this, you also need to give the form_tag method a :multipart => true parameter.

form_tag

Creates a form tag that wraps around other form elements but does not set context like form_for.

hidden_field_tag

Creates a hidden form field.

image_submit_tag

Not for submitting images, but rather for creating submit buttons that are presented as images.

label_tag

Creates a label.

password_field_tag

Creates a password field.

radio_button_tag

Creates a radio button.

select_tag

Creates a drop-down select or multiselect list.

submit_tag

Creates a submit button with a text caption.

text_area_tag

Creates a larger multiline text area.

text_field_tag

Creates a single-line text field.

The FormOptionsHelper methods are complementary to the methods in the other two form-related helper classes. Some of the methods (collection_select, country_select, select, and time_zone_select) take the same arguments as the field-creating methods in FormHelper and can be used the same way, with a context set by form_for or without. The other methods are focused on creating options for those methods, and may also be used to create option lists for the FormTagHelper’s select_tag method:

collection_select

Creates a select list from a specified object or array.

country_options_for_select

Creates option tags for an alphabetical list of countries, accepting arguments to indicate which should be selected and which should appear first in the list.

country_select

Creates a complete select list of countries including option tags. Also accepts arguments for a selected default and for giving countries higher priority in the list.

option_groups_from_collection_for_select

Creates option tags structured with optgroup tags based on an object or array.

options_for_select

Creates option tags based on a hash or array.

options_from_collection_for_select

Creates option tags based on a collection object.

select

Creates a complete select list including option tags.

time_zone_options_for_select

Returns option tags for time zones across the planet.

time_zone_select

Returns a complete select list for time zones across the planet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset