是否有更好的方法来适当地命名此字段? [英] Is there a better way to name this field appropriately?

查看:142
本文介绍了是否有更好的方法来适当地命名此字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表单代码(省略了不相关的部分):

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 %>

您可以在第四行看到哑巴部分:我正在将名称硬编码为user[paid_account_attributes][account_plan_id].如果我不这样做,它得到的名称是user[paid_account][account_plan_id],它不会在后端显示.

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?

推荐答案

在第二行中,删除resource.并仅放置:paid_account

In 2nd line remove resource. and put :paid_account only

<%= f.fields_for :paid_account do |pa| %>   

这篇关于是否有更好的方法来适当地命名此字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆