[英]Rails, collection_select and Proc
我有一個國家的州列表,我想在<select>
上顯示它們。 由於我試圖顯示一個代表每個狀態的標志的圖標,因此我正在使用此 JQuery插件。 無論如何,這是一個純粹的Rails問題。
要使用該插件,我需要為此select
包含的每個option
標簽設置一個“數據圖像”屬性。
到目前為止,我有:
<%= 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。 我會很感激的建議。
查看此Stackoverflow問題的第二個答案。
基本上,它涉及使用options_for_select
幫助器來創建自定義data
前綴的屬性。
因此,我最終解決了問題,以下是完整的解決方案。 據我了解,由於我使用的是Rails 2.3,因此沒有幫助者支持data
前綴屬性。 原來,我需要使用此答案指出的Rails 3 options_for_select
幫助器。 附帶說明,我沒有使用msDropDown插件,而最終使用了ddSlick 。 另外,我沒有使用collection_select
,而是使用了select_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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.