我想作为列名使用如何使用options_from_collection_for_select帮助器返回不同的值?
options_from_collection_for_select()
我有一个产品模型选择标签内恢复我的产品的国家,国家。我的代码看起来像这样
<%= select_tag(:country, options_from_collection_for_select(Product.all.order(:country), :id, :country), :prompt => "Alle Länder") %>
但是,它给了我每个国家不止一次的下拉字段,即每个国家出现20次。
现在,我一直在试图使用一个类似的问题提出了解决,像这样
options_from_collection_for_select(Product.all.pluck('DISTINCT country'), :id, :country), :prompt => "Alle Länder") %>
但是我得到一个错误,指出:
"undefined method `country' for "Spain":String
Did you mean? count"
现在我无法弄清楚哪些方法在这种情况下是适当的。
非常感谢!
当你使用'pluck'你已经得到的字符串。此外,我想这个查询会给你产品的ID,而不是国家。也许你的模型应该有country_id而不是国名?你可以尝试'select_tag(:country,Product.all.pluck('DISTINCT country'))' –