问题
I have the following form code (irrelevant parts omitted):
<%= 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 %>
You can see the dumb part on the 4th line: I'm hard-coding the name to user[paid_account_attributes][account_plan_id]
. If I don't do that, the name it gets is user[paid_account][account_plan_id]
, which doesn't fly on the back-end.
The way I'm doing it works, but it feels like a hack. Is there a more elegant way?
回答1:
In 2nd line remove resource.
and put :paid_account
only
<%= f.fields_for :paid_account do |pa| %>
来源:https://stackoverflow.com/questions/21891120/is-there-a-better-way-to-name-this-field-appropriately