1

我有一个国家/地区的州列表,我想在 a 上显示它们<select>。因为我试图显示一个代表每个州的标志的图标,所以我正在使用这个JQuery 插件。无论如何,在这一点上,这是一个纯粹的 Rails 问题。

要使用该插件,我需要option在 this 中包含的每个标签上设置一个“data-image”属性select

到目前为止,我所拥有的是:

<%= collection_select(:state, :code, @states, :code, 
:name, {"data-image" => lambda {|state| image_path("/flags/#{state.code}.png") }}) %>

我已经尝试了很多方法来为每个设置“数据图像”属性,option但到目前为止结果是:

<select id="state_code" name="state[code]">
  <option value="foo">State Foo</option>
  <option value="bar" selected="selected">State Bar</option>
</select>

因此,我没有成功注入我的“数据图像”属性。我一直在寻找一些光明,我看到了这个[api.rubyonrails] 和下面的例子,

collection_select(:post, :category_id, Category.all, :id, :name, {:disabled => lambda{|category| category.archived? }})

这基本上就是我要找的东西,我的东西几乎是一样的,但它不起作用。我正在使用 Rails 2.3.11。我会感谢你的建议。

4

2 回答 2

1

查看此 Stackoverflow 问题的第二个答案。

基本上,它涉及使用options_for_select帮助程序来创建自定义data前缀属性。

于 2013-01-06T18:05:12.740 回答
0

所以,我最终解决了这个问题,以下是完整的解决方案。据我了解,由于我使用的是 Rails 2.3,因此没有帮助程序来支持data前缀属性。事实证明,正如这个答案options_for_select所指出的那样,我需要使用 Rails 3助手。作为旁注,我没有使用 msDropDown 插件,而是最终使用了ddSlick。另外,我没有使用 ,而是使用了. 那么基本上你必须拥有的是:collection_selectselect_tag

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)) %>

这里,current_state是要选择作为默认选项的状态。例如,我将它存储在一个session变量中,但为了简单起见,它可能是一个@current_state.

解决此问题的另一种方法是使用options_from_collection_for_select_with_data的修改版本,因此无需公开map.

于 2013-01-07T20:25:55.207 回答