ActionView::Helpers::FormOptionsHelper

options_from_collection_for_select

API docs (Rails 8.1.4.1) ↗
def options_from_collection_for_select(collection, value_method, text_method, selected = nil)

Description

Creates an <option> for each item in collection.

Arguments

Changing a value updates the ERB and HTML.

collectionPlan.all
Calls this method on each item in collection and sets the value of the <option> to the return value. With a proc, calls it with the item and uses the return value.
Calls this method on each item in collection and uses the return value as the text of the <option>. With a proc, calls it with the item 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 item in collection and adds selected to the <option> of each item 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 value_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