So, I ended up solving the problem, following is the complete solution. As far as I understood, since I am using Rails 2.3 there are not helpers to support data
prefixed attributes. It turned out that I needed to use the Rails 3 options_for_select
helpers as pointed out by this answer. As a side note, instead of using the msDropDown plugin, and I ended up using the ddSlick. Also, instead of using collection_select
, I've used a select_tag
. Then basically what you have to have is:
rails_overrides.rb
# https://stackoverflow.com/a/13962481/914874
module RailsOverrides
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)
options_for_select = container.inject([]) do |options, element|
html_attributes = option_html_attributes(element)
text, value = option_text_and_value(element)
selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
options << %(<option value="#{html_escape(value.to_s)}"#{selected_attribute}#{disabled_attribute}#{html_attributes}>#{html_escape(text.to_s)}</option>)
end
options_for_select.join("\n").html_safe
end
def option_text_and_value(option)
# Options are [text, value] pairs or strings used for both.
case
when Array === option
option = option.reject { |e| Hash === e }
[option.first, option.last]
when !option.is_a?(String) && option.respond_to?(:first) && option.respond_to?(:last)
[option.first, option.last]
else
[option, option]
end
end
def option_html_attributes(element)
return "" unless Array === element
html_attributes = []
element.select { |e| Hash === e }.reduce({}, :merge).each do |k, v|
html_attributes << " #{k}=\"#{ERB::Util.html_escape(v.to_s)}\""
end
html_attributes.join
end
end
application_helper.rb
module ApplicationHelper
include RailsOverrides
end
index.html.erb
<%= select_tag(:state, options_for_select (states.map { |state| [state.name, state.code, {"data-imagesrc" => image_path("flags/#{state.code}.png"), "data-description" => "Data from #{state.name}"}]}, current_state.code)) %>
Here, current_state
is the state to be selected as the default option. For instance, I store it on a session
variable, but for the sake of simplicity it could be a @current_state
.
Another way to solve that would be to use a modified version of options_from_collection_for_select_with_data so it would no be necessary to expose the map
.