Type:
Class

A FormBuilder object is associated with a particular model object and allows you to generate fields associated with the model object. The FormBuilder object is yielded when using form_for or fields_for. For example:

<%= form_for @person do |person_form| %>
  Name: <%= person_form.text_field :name %>
  Admin: <%= person_form.check_box :admin %>
<% end %>

In the above block, the a FormBuilder object is yielded as the person_form variable. This allows you to generate the text_field and check_box fields by specifying their eponymous methods, which modify the underlying template and associates the +@person+ model object with the form.

The FormBuilder object can be thought of as serving as a proxy for the methods in the FormHelper module. This class, however, allows you to call methods with the model object you are building the form for.

You can create your own custom FormBuilder templates by subclassing this class. For example:

class MyFormBuilder < ActionView::Helpers::FormBuilder
  def div_radio_button(method, tag_value, options = {})
    @template.content_tag(:div,
      @template.radio_button(
        @object_name, method, tag_value, objectify_options(options)
      )
    )
  end

The above code creates a new method div_radio_button which wraps a div around the a new radio button. Note that when options are passed in, you must called objectify_options in order for the model object to get correctly passed to the method. If objectify_options is not called, then the newly created helper will not be linked back to the model.

The div_radio_button code from above can now be used as follows:

<%= form_for @person, :builder => MyFormBuilder do |f| %>
  I am a child: <%= f.div_radio_button(:admin, "child") %>
  I am an adult: <%= f.div_radio_button(:admin, "adult") %>
<% end -%>

The standard set of helper methods for form building are located in the field_helpers class attribute.

emitted_hidden_id?

emitted_hidden_id?() Instance Public methods

2015-06-20 00:00:00
fields_for

fields_for(record_name, record_object = nil, fields_options = {}, &block) Instance Public methods

2015-06-20 00:00:00
file_field

file_field(method, options = {}) Instance Public methods Returns a file upload

2015-06-20 00:00:00
radio_button

radio_button(method, tag_value, options = {}) Instance Public methods Returns

2015-06-20 00:00:00
grouped_collection_select

grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})

2015-06-20 00:00:00
date_select

date_select(method, options = {}, html_options = {}) Instance Public methods Wraps

2015-06-20 00:00:00
label

label(method, text = nil, options = {}, &block) Instance Public methods Returns

2015-06-20 00:00:00
to_partial_path

to_partial_path() Instance Public methods

2015-06-20 00:00:00
to_model

to_model() Instance Public methods

2015-06-20 00:00:00
check_box

check_box(method, options = {}, checked_value = "1", unchecked_value = "0") Instance Public methods

2015-06-20 00:00:00