ActionView::Helpers::FormOptionsHelper

options_for_select

API docs (Rails 8.1.4.1) ↗
def options_for_select(container, selected = nil)

Description

Creates <option> elements from an array or a hash.

Arguments

Changing a value updates the ERB and HTML.

Used as the text and the value of each <option>. Accepts:
  • An array of strings: the text and the value are the same.
  • An array of [text, value] pairs, or a hash (text => value): sets the text and the value separately.
  • A hash as the third element of a pair: becomes HTML attributes of that <option>.
Passing nil raises NoMethodError.
= 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 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.
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