method

select_tag

select_tag(name, option_tags = nil, options = {})
public

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.

  • :include_blank - If set to true, an empty option will be create

  • :prompt - Create a prompt option with blank value and the text asking user to select something

  • Any other key creates standard HTML attributes for the tag.

Examples

select_tag "people", options_from_collection_for_select(@people, "id", "name")
# <select id="people" name="people"><option value="1">David</option></select>

select_tag "people", "<option>David</option>".html_safe
# => <select id="people" name="people"><option>David</option></select>

select_tag "count", "<option>1</option><option>2</option><option>3</option><option>4</option>".html_safe
# => <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>".html_safe, :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>".html_safe
# => <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>".html_safe, :multiple => true, :class => 'form_input'
# => <select class="form_input" id="access" multiple="multiple" name="access[]"><option>Read</option>
#    <option>Write</option></select>

select_tag "people", options_from_collection_for_select(@people, "id", "name"), :include_blank => true
# => <select id="people" name="people"><option value=""></option><option value="1">David</option></select>

select_tag "people", options_from_collection_for_select(@people, "id", "name"), :prompt => "Select something"
# => <select id="people" name="people"><option value="">Select something</option><option value="1">David</option></select>

select_tag "destination", "<option>NYC</option><option>Paris</option><option>Rome</option>".html_safe, :disabled => true
# => <select disabled="disabled" id="destination" name="destination"><option>NYC</option>
#    <option>Paris</option><option>Rome</option></select>

10Notes

select_tag with options_for_select example

autonomous · Jul 22, 200819 thanks

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)

edavey · Jul 23, 20083 thanks

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

szeryf · Aug 18, 20093 thanks

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

andredurao · Aug 26, 20092 thanks

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} %>

Using an unobtrusive Ajax (UJS) :onchange call to the controller#action

gwshaw · Jan 4, 20132 thanks

An +:onchange+ call can be made using the Rails 3 jquery-ujs helper in the form: select_tag( name, option_tags, misc_html_options, html_5_data-stuff) For example: select_tag( "my_tag_id", get_ids(@entity), class: "progress_and_message", data: { remote: true, url: url_for( action: :my_controller_action, id: my_id) // application symbols progress_bar: "progress_bar_div_id", update: "message_div_id" } ) The jquery_ujs looks for +data-remote+ and +data-url+. These can be spelled out or placed in the data hash. The url must be formed as select_tag does not call url_for, unlike some of the other related tags. Values for application symbols can also be passed through. jQuery triggers will work on the Ajax events that occur. This generates the following: