我有以下表单代码
<%= f.fields_for resource.paid_account do |pa| %>
<%= pa.collection_select :account_plan_id, @account_plans, :id, :name_with_price %>
<% end %>
生成以下HTML
<select id="user_paid_account_account_plan_id" name="user[paid_account][account_plan_id]">
<option value="2">Lite ($10.00/mo)</option>
<option value="3">Professional ($20.00/mo)</option>
<option value="4">Plus ($30.00/mo)</option>
</select>
user[paid_account][account_plan_id]
是正确的名字吗?不应该是user[paid_account_attributes][account_plan_id]
?
我问,因为这会导致后端出现问题;我的account_plan
记录未创建。
答案 0 :(得分:0)
看起来这个答案是肯定的。我手动更改了这个名字:
<%= pa.collection_select :account_plan_id, @account_plans, :id, :name_with_price, {},
{ name: "user[paid_account_attributes][account_plan_id]" } %>
现在它有效。感觉就像一个黑客,所以似乎必须有一个更好的方法去做。