我正在创建一系列link_to,并且我将一些嵌套信息作为数组传递到每个URL中。我期望的结果如下:
?features%5B%5D%5BThick%5D=98&features%5B%5D%5BThin%5D=99
//For some legibility
?features[][Thick]=98&features[][Thin]=99
但是,数组内部哈希的键没有显示出来,我反而看到了:
?features%5B%5D%5B%5D=98&features%5B%5D%5B%5D=99
//For some legibility
?features[][]=98&features[][]=99
创建这一系列网址的erb就在这里:
<% @products.each |product| do %>
<%= link_to "", new_line_item_path(product_id: product, features: [product.features.each{|feature| {feature.name.to_sym => feature.feature_color_default}}])%>
<% end %>
这只是一个语法错误,还是因为我采取了错误的做法?
**也许这个问题太多了,但是产品有许多功能,而这些功能又通过Feature_Colors有很多颜色。
答案 0 :(得分:0)
我不确定为什么上面的内容不起作用,但我做了以下更改并且全部设定完毕。希望这里的关键字可以帮助其他人,如果他们犯同样的错误。
首先,我将哈希创建拉出到Product模型中,如下所示:
def default_features
list = Hash.new
features.each do |feature|
list[feature.name] = feature.feature_color_default_id
end
return list
end
然后我改变了link_to:
<%= link_to "", new_line_item_path(product_id: product, features: [product.default_features])%>
现在按需工作。