ActionView::Helpers::FormOptionsHelper

grouped_options_for_select

API docs (Rails 8.1.4.1) ↗
def grouped_options_for_select(grouped_options, selected_key = nil, options = {})

Description

Creates an <optgroup> for each group, containing its <option> elements.

Arguments

Changing a value updates the ERB and HTML.

Creates one <optgroup> for each item. Accepts:
  • A hash (group name => choices): uses each key as the label of its <optgroup>.
  • An array of [group name, choices] pairs: uses the first element as the label of the <optgroup>. A hash as the third element of a pair becomes HTML attributes of that <optgroup>.
  • An array of arrays that hold only choices: used with the :divider option. Without :divider, the first element of each array is used as the group name and the second as the choices, and an array with only one choice raises NoMethodError.
The choices use the same format as the container argument of options_for_select (an array of strings, an array of [text, value] pairs, etc.).
= nil
Passed as is to the selected argument of options_for_select for each group. Takes a value, an array of values, or a hash with selected and disabled keys. If more than one group has an <option> with the same value, all of them get selected.
options= {}
true adds an <option> with an empty value at the top. Its text is the helpers.select.prompt translation, or Please select if there is none. A string sets its text. Unlike in select, it is added even when an <option> has selected.
"----------"
Sets the label of every <optgroup> to this string instead of the group name. With this option, each element of grouped_options is read as an array of choices without a group name.
Options not listed here are ignored.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview

Interacting with the preview updates the form submission.

Form submission

Form data

namevalue
Request body

Content-Type:


              

params