Is there a better way to name this field appropriately?

痞子三分冷 提交于 2019-12-11 23:15:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!