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.
  • 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>
Show source
Register or log in to add new notes.
July 22, 2008
9 thanks

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:

  <select id="user_id" name="user_id">
    <option value="1">Brad</option>
    <option value="2">Angie</option>
    <option value="3">Jenny</option>
  </select>
July 23, 2008
2 thanks

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:

 <select id="region_3" name="region[3][country_id]">
   <option value="0">unassigned</option>
   <option selected="selected" value="12">England</option>
 </select>

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.

July 25, 2008 - (v1.0.0 - v2.1.0)
1 thank

select_options_tag - no more worries...

no more explicit options_for_select calls..

  def select_options_tag(name='',select_options={},options={})
    #set selected from value
    selected = ''
    unless options[:value].blank?
      selected = options[:value]
      options.delete(:value)
    end
    select_tag(name,options_for_select(select_options,selected),options)
  end

select_options_tag(‘name’,[[‘oh’,’no’]],:value=>’no’)