select_tag

select_tag(name, option_tags = nil, options = {})
Instance Public methods

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 created.

  • :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>

select_tag "credit_card", options_for_select([ "VISA", "MasterCard" ], "MasterCard")
# => <select id="credit_card" name="credit_card"><option>VISA</option>
#    <option selected="selected">MasterCard</option></select>
doc_ruby_on_rails
2015-06-20 00:00:00
Comments
Leave a Comment

Please login to continue.