instance method grouped_options_for_select

Ruby on Rails 4.2.9

Since v2.3.18

Available in: v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

grouped_options_for_select(grouped_options, selected_key = nil, options = {})

Returns a string of <option> tags, like options_for_select, but wraps them with <optgroup> tags:

grouped_options = [
 ['North America',
   [['United States','US'],'Canada']],
 ['Europe',
   ['Denmark','Germany','France']]
]
grouped_options_for_select(grouped_options)

grouped_options = {
  'North America' => [['United States','US'], 'Canada'],
  'Europe' => ['Denmark','Germany','France']
}
grouped_options_for_select(grouped_options)

Possible output:

<optgroup label="North America">
  <option value="US">United States</option>
  <option value="Canada">Canada</option>
</optgroup>
<optgroup label="Europe">
  <option value="Denmark">Denmark</option>
  <option value="Germany">Germany</option>
  <option value="France">France</option>
</optgroup>

Parameters:

  • grouped_options - Accepts a nested array or hash of strings. The first value serves as the <optgroup> label while the second value must be an array of options. The second value can be a nested array of text-value pairs. See options_for_select for more info.

    Ex. ["North America",[["United States","US"],["Canada","CA"]]]
  • selected_key - A value equal to the value attribute for one of the <option> tags, which will have the selected attribute set. Note: It is possible for this value to match multiple options as you might have the same option in multiple groups. Each will then get selected="selected".

Options:

  • :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.

  • :divider - the divider for the options groups.

    grouped_options = [
      [['United States','US'], 'Canada'],
      ['Denmark','Germany','France']
    ]
    grouped_options_for_select(grouped_options, nil, divider: '---------')
    

    Possible output:

    <optgroup label="---------">
      <option value="US">United States</option>
      <option value="Canada">Canada</option>
    </optgroup>
    <optgroup label="---------">
      <option value="Denmark">Denmark</option>
      <option value="Germany">Germany</option>
      <option value="France">France</option>
    </optgroup>

Note: Only the <optgroup> and <option> tags are returned, so you still have to wrap the output in an appropriate <select> tag.

Parameters

grouped_options req
selected_key opt = nil
options opt = {}
Source
# File actionview/lib/action_view/helpers/form_options_helper.rb, line 524
      def grouped_options_for_select(grouped_options, selected_key = nil, options = {})
        prompt  = options[:prompt]
        divider = options[:divider]

        body = "".html_safe

        if prompt
          body.safe_concat content_tag(:option, prompt_text(prompt), value: "")
        end

        grouped_options.each do |container|
          html_attributes = option_html_attributes(container)

          if divider
            label = divider
          else
            label, container = container
          end

          html_attributes = { label: label }.merge!(html_attributes)
          body.safe_concat content_tag(:optgroup, options_for_select(container, selected_key), html_attributes)
        end

        body
      end

Defined in actionview/lib/action_view/helpers/form_options_helper.rb line 524 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::FormOptionsHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close