public Method

FormTagHelper.select_tag(name, option_tags = nil, options = {})

Contents:

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 name="people"><option>David</option></select>

select_tag "count", "<option>1</option><option>2</option><option>3</option><option>4</option>"
# => <select 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 name="colors" multiple="multiple"><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 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 name="access" multiple="multiple"><option>Read</option>
#    <option>Write</option></select>

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

Source Code

# File action_view/helpers/form_tag_helper.rb, line 79
def select_tag(name, option_tags = nil, options = {})
  content_tag :select, option_tags, { "name" => name, "id" => name }.update(options.stringify_keys)
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.