我有以下表格代码(省略了相关部分):
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { id: "payment-form" }) do |f| %>
<%= f.fields_for resource.paid_account do |pa| %>
<%= pa.collection_select :account_plan_id, @account_plans, :id, :name_with_price, {},
{ name: "user[paid_account_attributes][account_plan_id]" } %>
<% end %>
<% end %>
你可以在第4行看到愚蠢的部分:我正在将名称硬编码为user[paid_account_attributes][account_plan_id]
。如果我不这样做,它获得的名称是user[paid_account][account_plan_id]
,它不会在后端飞行。
我这样做的方式有效,但感觉就像是黑客。有更优雅的方式吗?
答案 0 :(得分:0)
在第二行中删除resource.
并仅添加:paid_account
<%= f.fields_for :paid_account do |pa| %>