Returns a string of option tags that have been compiled by iterating over
the collection
and assigning the result of a call to the
value_method
as the option value and the
text_method
as the option text.
1 2 | options_from_collection_for_select( @people , 'id' , 'name' ) # => <option value="#{person.id}">#{person.name}</option> |
This is more often than not used inside a select_tag like this example:
1 | select_tag 'person' , options_from_collection_for_select( @people , 'id' , 'name' ) |
If selected
is specified as a value or array of values, the
element(s) returning a match on value_method
will be selected
option tag(s).
If selected
is specified as a Proc, those members of the
collection that return true for the anonymous function are the selected
values.
selected
can also be a hash, specifying both
:selected
and/or :disabled
values as required.
Be sure to specify the same class as the value_method
when
specifying selected or disabled options. Failure to do this will produce
undesired results. Example:
1 | options_from_collection_for_select( @people , 'id' , 'name' , '1' ) |
Will not select a person with the id of 1 because 1 (an Integer) is not the same as '1' (a string)
1 | options_from_collection_for_select( @people , 'id' , 'name' , 1 ) |
should produce the desired results.
Please login to continue.