ActionView::Helpers::FormOptionsHelper
def select(object, method, choices = nil, options = {}, html_options = {}, &block)

Description

Creates a <select> bound to a model attribute, with its <option> elements.

Arguments

Changing a value updates the ERB and HTML.

object:user
method:plan
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>.
  • A hash whose values are arrays (group name => choices), or an array of [group name, choices] pairs: groups the choices into <optgroup> elements. A hash as the third element of a pair becomes HTML attributes of that <optgroup>.
  • An html_safe string (such as the output of options_for_select): used as the content of the <select> as is. The object's value then adds no selected.
Adds selected to the <option> whose value matches the object's value. Passing nil raises NoMethodError. If the object's value is nil, adds selected to the <option> with an empty value.
options= {}
"team"
Instead of using the object's value, adds selected to each <option> whose value matches the given value (or, for an array, any value in it).
["team"]
Adds disabled to each <option> whose value matches the given value (or, for an array, any value in it).
true adds an <option> with an empty value at the top. A string sets its text. Passing false raises ArgumentError when the :required option in html_options is true, unless :multiple is true or :size is not 1.
true adds an <option> with an empty value at the top, but only when the object's value (or the :selected option, if given) is empty. Its text is the helpers.select.prompt translation, or Please select if there is none. A string sets its text.
false omits the hidden <input> with an empty value that is otherwise added when the :multiple option in html_options is true.
Options not listed here are ignored, except required, multiple and size, which are moved to html_options and become HTML attributes of the <select>.
html_options= {}
true adds the multiple attribute and appends [] to name. Also adds a hidden <input> with an empty value before the <select>.
true adds the required attribute, plus an <option> with an empty value at the top even without the :include_blank option. The blank <option> is left out when a truthy :prompt option is given, or when the :multiple option in html_options is true or :size is not 1.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <select>, and ones this site considers rarely used, are not listed here.

Shared by form helpers

"q"
Replaces the generated name with the given value. nil omits name.
nil
Replaces the generated id with the given value. nil omits id.

Shared by all helpers

true adds disabled="disabled"; false omits it. The same goes for every HTML attribute in Rails' list (BOOLEAN_ATTRIBUTES), such as readonly, required and autofocus: any value other than false or nil adds it as attr="attr".
{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
["section-user", "name"]
An array sets autocomplete to its elements joined with spaces.
Any other html_options become HTML attributes of the <select>; those with a nil value are left out.

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