Method: ActionView::Helpers::FormOptionsHelper#select
- Defined in:
- lib/action_view/helpers/form_options_helper.rb
#select(object, method, choices, options = {}, html_options = {}) ⇒ Object
Create a select tag and a series of contained option tags for the provided object and method. The option currently held by the object will be selected, provided that the object is available. See options_for_select for the required format of the choices parameter.
Example with @post.person_id => 1:
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })
could become:
<select name="post[person_id]">
<option value=""></option>
<option value="1" selected="selected">David</option>
<option value="2">Sam</option>
<option value="3">Tobias</option>
</select>
This can be used to provide a default set of options in the standard way: before rendering the create form, a new model instance is assigned the default options and bound to @model_name. Usually this model is not saved to the database. Instead, a second model object is created when the create request is received. This allows the user to submit a form page more than once with the expected results of creating multiple records. In addition, this allows a single partial to be used to generate form inputs for both edit and create forms.
By default, post.person_id is the selected option. Specify :selected => value to use a different selection or :selected => nil to leave all options unselected. Similarly, you can specify values to be disabled in the option tags by specifying the :disabled option. This can either be a single value or an array of values to be disabled.
131 132 133 |
# File 'lib/action_view/helpers/form_options_helper.rb', line 131 def select(object, method, choices, = {}, = {}) InstanceTag.new(object, method, self, .delete(:object)).to_select_tag(choices, , ) end |