Module: ActionView::Helpers::FormOptionsHelper

Defined in:
lib/enhanced_select.rb

Instance Method Summary collapse

Instance Method Details

#enhanced_options_for_select(options = [], selected = nil) ⇒ Object

Examples (call, result):

enhanced_options_for_select [{:value => 1, :text => "New York"}, {:value => 2, :text => "Denver"}]
<option value="1">New York</option>\n<option value="2">Denver</option>

enhanced_options_for_select [{:value => "bmw", :text => "BMW"}, {:value => "tesla", :text => "Tesla Motors"}], "tesla"
<option value="bmw">BMW</option>\n<option value="tesla" selected="selected">Tesla Motors</option>

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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/enhanced_select.rb', line 21

def enhanced_options_for_select options = [], selected = nil
				# Ensure option selections are unique.
				options.each {|option| option.delete :selected}
				# Generate the HTML code.
  options.map do |option|
					option[:selected] = "selected" if option[:value] == selected
					html = "<option"
					option.each_pair {|key, value| html << build_attribute(key, value)}
					html << ">#{html_escape option[:text].to_s}</option>\n"
  end.to_s
end

#enhanced_select(object, method, choices, options = {}, html_options = {}) ⇒ Object



5
6
7
# File 'lib/enhanced_select.rb', line 5

def enhanced_select object, method, choices, options = {}, html_options = {}
  InstanceTag.new(object, method, self, options.delete(:object)).to_enhanced_select_tag(choices, options, html_options)
end