Type:
Module

Provides a number of methods for turning different kinds of containers into a set of option tags.

The collection_select, select and time_zone_select methods take an options parameter, a hash:

  • :include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.

    1
    select("post", "category", Post::CATEGORIES, {include_blank: true})

    could become:

    1
    2
    3
    4
    5
    <select name="post[category]">
      <option></option>
      <option>joke</option>
      <option>poem</option>
    </select>

    Another common case is a select tag for a belongs_to-associated object.

    Example with @post.person_id => 2:

    1
    select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: 'None'})

    could become:

    1
    2
    3
    4
    5
    6
    <select name="post[person_id]">
      <option value="">None</option>
      <option value="1">David</option>
      <option value="2" selected="selected">Sam</option>
      <option value="3">Tobias</option>
    </select>
  • :prompt - set to true or a prompt string. When the select element doesn't have a value yet, this prepends an option with a generic prompt – “Please select” – or the given prompt string.

    1
    select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {prompt: 'Select Person'})

    could become:

    1
    2
    3
    4
    5
    6
    <select name="post[person_id]">
      <option value="">Select Person</option>
      <option value="1">David</option>
      <option value="2">Sam</option>
      <option value="3">Tobias</option>
    </select>
  • :index - like the other form helpers, select can accept an :index option to manually set the ID used in the resulting output. Unlike other helpers, select expects this option to be in the html_options parameter.

    1
    select("album[]", "genre", %w[rap rock country], {}, { index: nil })

    becomes:

    1
    2
    3
    4
    5
    <select name="album[][genre]" id="album__genre">
      <option value="rap">rap</option>
      <option value="rock">rock</option>
      <option value="country">country</option>
    </select>
  • :disabled - can be a single value or an array of values that will be disabled options in the final output.

    1
    select("post", "category", Post::CATEGORIES, {disabled: 'restricted'})

    could become:

    1
    2
    3
    4
    5
    6
    <select name="post[category]">
      <option></option>
      <option>joke</option>
      <option>poem</option>
      <option disabled="disabled">restricted</option>
    </select>

    When used with the collection_select helper, :disabled can also be a Proc that identifies those options that should be disabled.

    1
    collection_select(:post, :category_id, Category.all, :id, :name, {disabled: lambda{|category| category.archived? }})

    If the categories “2008 stuff” and “Christmas” return true when the method archived? is called, this would return:

    1
    2
    3
    4
    5
    6
    <select name="post[category_id]">
      <option value="1" disabled="disabled">2008 stuff</option>
      <option value="2" disabled="disabled">Christmas</option>
      <option value="3">Jokes</option>
      <option value="4">Poems</option>
    </select>
collection_radio_buttons
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

collection_radio_buttons(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)

2025-01-10 15:47:30
collection_check_boxes
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)

2025-01-10 15:47:30
grouped_options_for_select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

grouped_options_for_select(grouped_options, selected_key = nil, options = {}) Instance Public methods

2025-01-10 15:47:30
collection_select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) Instance

2025-01-10 15:47:30
select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

select(object, method, choices = nil, options = {}, html_options = {}, &block) Instance Public methods

2025-01-10 15:47:30
options_for_select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

options_for_select(container, selected = nil) Instance Public methods Accepts

2025-01-10 15:47:30
options_from_collection_for_select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

options_from_collection_for_select(collection, value_method, text_method, selected = nil) Instance Public methods

2025-01-10 15:47:30
time_zone_options_for_select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone) Instance Public

2025-01-10 15:47:30
option_groups_from_collection_for_select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil)

2025-01-10 15:47:30
grouped_collection_select
  • References/Ruby on Rails/Rails/Classes/ActionView/ActionView::Helpers/ActionView::Helpers::FormOptionsHelper

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

2025-01-10 15:47:30