ActionView::Helpers::FormOptionsHelper

option_groups_from_collection_for_select

API docs (Rails 8.1.4.1) ↗
def option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil)

Description

Creates an <optgroup> for each item in collection, containing its <option> elements.

Arguments

Changing a value updates the ERB and HTML.

collectionPlanGroup.all
Creates one <optgroup> for each item.
Calls this method on each item in collection and turns each element of the returned array into an <option> in that <optgroup>. With a proc, calls it with the item and uses the return value.
Calls this method on each item in collection and sets the label of the <optgroup> to the return value. With a proc, calls it with the item and uses the return value.
Calls this method on each element of the array that group_method returns, and sets the value of the <option> to the return value. With a proc, calls it with the element and uses the return value.
Calls this method on each element of the array that group_method returns, and uses the return value as the text of the <option>. With a proc, calls it with the element and uses the return value.
= nil
Accepts:
  • A value: adds selected to the <option> whose value matches it.
  • An array of values: adds selected to each <option> whose value matches any of them.
  • A proc: calls it for each element of the array that group_method returns, and adds selected to the <option> of each element for which it returns a truthy value.
  • A hash with selected and disabled keys: adds selected to each <option> whose value matches the selected key's value, and disabled to each one whose value matches the disabled key's value. Both keys also take a proc.
Passing a proc raises TypeError when option_key_method is a proc. Values are compared as strings, so 2 and "2" are treated as equal.

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