Method: ActionView::Helpers::FormOptionsHelper#options_for_select

Defined in:
lib/action_view/helpers/form_options_helper.rb

#options_for_select(container, selected = nil) ⇒ Object

Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container where the elements respond to first and last (such as a two-element array), the “lasts” serve as option values and the “firsts” as option text. Hashes are turned into this form automatically, so the keys become “firsts” and values become lasts. If selected is specified, the matching “last” or element will get the selected option-tag. selected may also be an array of values to be selected when using a multiple select.

Examples (call, result):

options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])
  <option value="$">Dollar</option>\n<option value="DKK">Kroner</option>

options_for_select([ "VISA", "MasterCard" ], "MasterCard")
  <option>VISA</option>\n<option selected="selected">MasterCard</option>

options_for_select({ "Basic" => "$20", "Plus" => "$40" }, "$40")
  <option value="$20">Basic</option>\n<option value="$40" selected="selected">Plus</option>

options_for_select([ "VISA", "MasterCard", "Discover" ], ["VISA", "Discover"])
  <option selected="selected">VISA</option>\n<option>MasterCard</option>\n<option selected="selected">Discover</option>

You can optionally provide html attributes as the last element of the array.

Examples:

options_for_select([ "Denmark", ["USA", {:class=>'bold'}], "Sweden" ], ["USA", "Sweden"])
  <option value="Denmark">Denmark</option>\n<option value="USA" class="bold" selected="selected">USA</option>\n<option value="Sweden" selected="selected">Sweden</option>

options_for_select([["Dollar", "$", {:class=>"bold"}], ["Kroner", "DKK", {:onclick => "alert('HI');"}]])
  <option value="$" class="bold">Dollar</option>\n<option value="DKK" onclick="alert('HI');">Kroner</option>

If you wish to specify disabled option tags, set selected to be a hash, with :disabled being either a value or array of values to be disabled. In this case, you can use :selected to specify selected option tags.

Examples:

options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], :disabled => "Super Platinum")
  <option value="Free">Free</option>\n<option value="Basic">Basic</option>\n<option value="Advanced">Advanced</option>\n<option value="Super Platinum" disabled="disabled">Super Platinum</option>

options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], :disabled => ["Advanced", "Super Platinum"])
  <option value="Free">Free</option>\n<option value="Basic">Basic</option>\n<option value="Advanced" disabled="disabled">Advanced</option>\n<option value="Super Platinum" disabled="disabled">Super Platinum</option>

options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], :selected => "Free", :disabled => "Super Platinum")
  <option value="Free" selected="selected">Free</option>\n<option value="Basic">Basic</option>\n<option value="Advanced">Advanced</option>\n<option value="Super Platinum" disabled="disabled">Super Platinum</option>

NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/action_view/helpers/form_options_helper.rb', line 297

def options_for_select(container, selected = nil)
  return container if String === container

  container = container.to_a if Hash === container
  selected, disabled = extract_selected_and_disabled(selected).map do | r |
     Array.wrap(r).map(&:to_s)
  end

  container.map do |element|
    html_attributes = option_html_attributes(element)
    text, value = option_text_and_value(element).map(&:to_s)
    selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
    disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
    %(<option value="#{html_escape(value)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{html_escape(text)}</option>)
  end.join("\n").html_safe

end