Method: ActionView::Helpers::FormTagHelper#select_tag
- Defined in:
- actionview/lib/action_view/helpers/form_tag_helper.rb
#select_tag(name, option_tags = nil, options = {}) ⇒ Object
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. If set to a string, the string will be used as the option’s content and the value will be empty. -
: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", (@people, "id", "name")
# <select id="people" name="people"><option value="1">David</option></select>
select_tag "people", (@people, "id", "name", "1")
# <select id="people" name="people"><option value="1" selected="selected">David</option></select>
select_tag "people", raw("<option>David</option>")
# => <select id="people" name="people"><option>David</option></select>
select_tag "count", raw("<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", raw("<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", raw("<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", raw("<option>Read</option><option>Write</option>"), multiple: true, class: 'form_input', id: 'unique_id'
# => <select class="form_input" id="unique_id" multiple="multiple" name="access[]"><option>Read</option>
# <option>Write</option></select>
select_tag "people", (@people, "id", "name"), include_blank: true
# => <select id="people" name="people"><option value="" label=" "></option><option value="1">David</option></select>
select_tag "people", (@people, "id", "name"), include_blank: "All"
# => <select id="people" name="people"><option value="">All</option><option value="1">David</option></select>
select_tag "people", (@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", raw("<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>
select_tag "credit_card", ([ "VISA", "MasterCard" ], "MasterCard")
# => <select id="credit_card" name="credit_card"><option>VISA</option>
# <option selected="selected">MasterCard</option></select>
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'actionview/lib/action_view/helpers/form_tag_helper.rb', line 201 def select_tag(name, = nil, = {}) ||= "" html_name = ([:multiple] == true && !name.end_with?("[]")) ? "#{name}[]" : name if .include?(:include_blank) include_blank = [:include_blank] = .except(:include_blank) = { value: "" } if include_blank == true include_blank = "" [:label] = " " end if include_blank = content_tag("option", include_blank, ).safe_concat() end end if prompt = .delete(:prompt) = content_tag("option", prompt, value: "").safe_concat() end content_tag "select", , { "name" => html_name, "id" => sanitize_to_id(name) }.update(.stringify_keys) end |