Method: SimpleForm::FormBuilder#collection_radio_buttons

Defined in:
lib/simple_form/form_builder.rb

#collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object

Create a collection of radio inputs for the attribute. Basically this helper will create a radio input associated with a label for each text/value option in the collection, using value_method and text_method to convert these text/value. You can give a symbol or a proc to both value_method and text_method, that will be evaluated for each item in the collection.

Examples

form_for @user do |f|
  f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end

<input id="user_options_true" name="user[options]" type="radio" value="true" />
<label class="collection_radio_buttons" for="user_options_true">Yes</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio_buttons" for="user_options_false">No</label>

It is also possible to give a block that should generate the radio + label. To wrap the radio with the label, for instance:

form_for @user do |f|
  f.collection_radio_buttons(
    :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
  ) do |b|
    b.label { b.radio_button + b.text }
  end
end

Options

Collection radio accepts some extra options:

* checked  => the value that should be checked initially.

* disabled => the value or values that should be disabled. Accepts a single
              item or an array of items.

* collection_wrapper_tag   => the tag to wrap the entire collection.

* collection_wrapper_class => the CSS class to use for collection_wrapper_tag

* item_wrapper_tag         => the tag to wrap each item in the collection.

* item_wrapper_class       => the CSS class to use for item_wrapper_tag

* a block                  => to generate the label + radio or any other component.
[View source] [View on GitHub]

397
398
399
# File 'lib/simple_form/form_builder.rb', line 397

def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  SimpleForm::Tags::CollectionRadioButtons.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)).render(&block)
end