这个嵌套的表单字段是否贴错标签?

时间:2014-02-19 19:16:26

标签: ruby-on-rails nested-attributes

我有以下表单代码

<%= 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记录未创建。

1 个答案:

答案 0 :(得分:0)

看起来这个答案是肯定的。我手动更改了这个名字:

<%= pa.collection_select :account_plan_id, @account_plans, :id, :name_with_price, {},
                         { name: "user[paid_account_attributes][account_plan_id]" } %>

现在它有效。感觉就像一个黑客,所以似乎必须有一个更好的方法去做。