我有以下数组:
[{:gig_id=>"1234BN-92846",
:albums_sold=>
[{:id=>"all-music", :name=>"All Music"},
{:id=>"presto", :name=>"Presto"},
{:id=>"hello", :name=>"Hello"}],
:clothing_sold=>
[{:id=>"all-clothing", :name=>"All Clothing"},
{:id=>"cool-shirt", :name=>"Cool Shirt"},
{:id=>"manchester-united-hoodie", :name=>"Manchester United Hoodie"},
{:id=>"manchester-united-tank-top", :name=>"Manchester United Tank-Top"},
{:id=>"manchester-united-hat", :name=>"Manchester United Hat"},
{:id=>"manchester-united-shirt", :name=>"Manchester United Shirt"}],
:promos_sold=>
[{:id=>"all-promo", :name=>"All Promo"},
{:id=>"jb-bobblehead", :name=>"JB Bobblehead"},
{:id=>"poster", :name=>"Poster"}]},
{:gig_id=>"1234BN-78234",
:clothing_sold=>
[{:id=>"all-clothing", :name=>"All Clothing"}, {:id=>"cool-shirt", :name=>"Cool Shirt"}]}]
我将select_tag定义为:
<%= select_tag "All Music, options_from_collection_for_select(name_object[:albums_sold], :id, :name) %>
其中name_object是一个定义为的环境变量:
<% name_object = @band.all_merch_items_names.find { |key| key[:gig_id] == gig } %>
Band#all_merch_items_names在顶部生成数组。
我一直收到错误:
undefined method `name' for {"id"=>"all-music", "name"=>"All Music"}:Hash
我尝试将密钥更改为符号(&#39; id&#39;,&#39; name&#39;)并在options_from_collection_for_select中更改它们以及&#39; id&#39;和&#39; name&#39;但我一直得到同样的错误。
我想要达到的目的是创建一个ID为附加到每个元素的下拉列表,以便我可以隐藏/显示具有相同ID的div。 。如果有更好的方法来实施select_tag,请建议。
任何帮助都会很棒!感谢。
答案 0 :(得分:1)
使用options_for_select修复问题:
<%= select_tag "All Music, options_for_select(name_object[:albums_sold].collect {|item| [item[:name], item[:id]]}) %>