select_tag
select_tag(name, option_tags = nil, options = {})Creates a dropdown selection box, or if the :multiple option is set to true, a multiple choice selection box.
Helpers::FormOptions can be used to create common select boxes such as countries, time zones, or associated records. option_tags is a string containing the option tags for the select box.
Options
- :multiple - If set to true the selection will allow multiple choices.
- :disabled - If set to true, the user will not be able to use this input.
- Any other key creates standard HTML attributes for the tag.
Examples
select_tag "people", "<option>David</option>" # => <select id="people" name="people"><option>David</option></select> select_tag "count", "<option>1</option><option>2</option><option>3</option><option>4</option>" # => <select id="count" name="count"><option>1</option><option>2</option> # <option>3</option><option>4</option></select> select_tag "colors", "<option>Red</option><option>Green</option><option>Blue</option>", :multiple => true # => <select id="colors" multiple="multiple" name="colors[]"><option>Red</option> # <option>Green</option><option>Blue</option></select> select_tag "locations", "<option>Home</option><option selected="selected">Work</option><option>Out</option>" # => <select id="locations" name="locations"><option>Home</option><option selected='selected'>Work</option> # <option>Out</option></select> select_tag "access", "<option>Read</option><option>Write</option>", :multiple => true, :class => 'form_input' # => <select class="form_input" id="access" multiple="multiple" name="access[]"><option>Read</option> # <option>Write</option></select> select_tag "destination", "<option>NYC</option><option>Paris</option><option>Rome</option>", :disabled => true # => <select disabled="disabled" id="destination" name="destination"><option>NYC</option> # <option>Paris</option><option>Rome</option></select>
8Notes
select_tag with options_for_select example
An example of using options_for_select with select_tag select_tag 'user_id', options_for_select(@users.collect{ |u| [u.name, u.id] })
This would generate something like:
options_for_select further example (using a collection and with a default value)
In this example, we are editing a collection of region records, each with its own select list of countries. (Region belongs_to :country.) If the region doesn't have a country associated, then we want a default message of "unassigned". Of course, if the region does have a country associated then we want that country displayed:
<% name = "region[" + region.id.to_s + "][country_id]" %> <% id = "region_" + region.id.to_s %>
<%= select_tag(id, options_for_select([["unassigned" , "0" ]] + Country.to_dropdown, region.country_id), {:name => name} ) %> This give us:
NB: we're using the handy +acts_as_dropdown+ plugin (http://delynnberry.com/projects/acts-as-dropdown/) but we could just as easily prepare the select list with +map+ / +collect+ as above.
Auto-submitting select tag
If you want your form to be submitted when user selects something, use:
:onchange => "this.form.submit();"
For example:
select_tag "people", "<option>David</option>", :onchange => "this.form.submit();"
sending an array of multiple options
To make sure that you'll receive a array you should declare the name of the select with "[ ]" like that:
==== Example
<%= select_tag "users[]", options_for_select(@users.collect{|x| [x.name,x.id]}), {:multiple => :multiple, :size => 10} %>
Setting name and id for select_tag
Sometimes you need to use select_tag instead of select (because you're after more control or need to use optgroups, for example), but still want the id/name conventions that select would give.
In this case, all you need to do is set the first parameter to whatever would be produced by select, and it'll take care of the id and name attribute automatically, and thus ensure the form data is parsed correctly after submission.
For example, if you want to do something like: form_for :comment do |f| f.select :article_id ...
which would give a select tag with id of "comment_article_id" and a name attribute of "comment[article_id]", which be parsed into the params hash of: 'comment' => {'article_id' => ...
you can instead do form_for :comment do |f| select_tag 'comment[article_id]' ...
which will give the same id and name attributes for the select tag and hence the same params hash in the controller
How to perform :onchange ajax call using jQuery (Rails 3)
As remote_function no longer exists in Rails 3, here is some handy substitution
<%= select_tag :assignee_id, options_for_select([["A",1],["B",2]]), :onchange => "$.post('#{model_singular_path(id)}', {'_method':'put', 'model_name[model_field]':this.value} );" %>
You can obviously pass more attributes, change method etc.
Showing the select with a value previously known
Enter the value in the 'value to check if exist in the list' section and the drop down should have that selected
==== Code example
select_tag "name",
options_for_select(list.collect{ [ text, value] },
'value to check if exist in the list',
{:include_blank => true}
multiple select has a hidden
It's unclear if "select_tag" does this but the normal select method also generates a hidden variable if the "multiple" option is set, see http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select