为什么<输入>是元素 ID 以嵌套形式重复?

发布于 2024-10-07 03:20:02 字数 1070 浏览 0 评论 0原文

class User
    has_one :user_profile
end
class UserProfile
    belongs_to :user
end

我正在渲染一个用于使用部分进行编辑的表单。

意见是:

user_profile/edit.html.erb
--------------------------
<%= render 'form' %>

user_profile/_form.html.erb
---------------------------
<%= form_for @user_profile do |f| %>
  <%= f.fields_for @user_profile do |builder| %>
    <%= render :partial => 'user_profiles/fields', :locals => { :f => builder } %>
    # id is correct i.e. [user_profile][last_name] for the field below 
    <%= f.text_field :last_name %>
  <% end %>
  <%= f.submit %>
<% end %>


user_profile/_fields.html.erb
-----------------------------
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>

# id's for the above fields are rendered as:
# user_profile[user_profile][first_name]
# notice the second repeated [user_profile]

如何修复它,以便 id 显示为 user_profile[first_name] 而不是 user_profile[user_profile][first_name]

class User
    has_one :user_profile
end
class UserProfile
    belongs_to :user
end

I am rendering a form for editing using partials.

The views are:

user_profile/edit.html.erb
--------------------------
<%= render 'form' %>

user_profile/_form.html.erb
---------------------------
<%= form_for @user_profile do |f| %>
  <%= f.fields_for @user_profile do |builder| %>
    <%= render :partial => 'user_profiles/fields', :locals => { :f => builder } %>
    # id is correct i.e. [user_profile][last_name] for the field below 
    <%= f.text_field :last_name %>
  <% end %>
  <%= f.submit %>
<% end %>


user_profile/_fields.html.erb
-----------------------------
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>

# id's for the above fields are rendered as:
# user_profile[user_profile][first_name]
# notice the second repeated [user_profile]

How do I fix it so that the id's come out as user_profile[first_name] instead of user_profile[user_profile][first_name] ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-10-14 03:20:02

fields_for 的文档中可以看出:

适合以相同的形式指定附加模型对象。

用一个显示此范例的示例:

<%= form_for @person do |person_form| %>
  First name: <%= person_form.text_field :first_name %>
  Last name : <%= person_form.text_field :last_name %>

  <%= fields_for @person.permission do |permission_fields| %>
    Admin?  : <%= permission_fields.check_box :admin %>
  <% end %>
<% end %>

注意 form_for @personfields_for @person.permission - fields_for 应该用于模型的关联,form_for 应该用于模型本身。您的 html 呈现为 user_profile[user_profile] 的原因是您在 form_forfields_for@user_profile代码>.

您可能需要将 form_for 中的参数更改为 @user (或范围内的等效变量)。这将使您的输出为 html:

user[user_profile][first_name]

只要用户 accepts_nested_attributes_for user_profile,您就可以将其传递给 UsersController#update。如果您想将参数哈希从表单传递到 UserProfilesController#update,那么您需要完全删除 fields_for 并仅渲染 form_for @user_profile 下的字段 - 这会给你像user_profile[first_name]这样的html。

From the documentation for fields_for it's:

suitable for specifying additional model objects in the same form.

With an example showing this paradigm:

<%= form_for @person do |person_form| %>
  First name: <%= person_form.text_field :first_name %>
  Last name : <%= person_form.text_field :last_name %>

  <%= fields_for @person.permission do |permission_fields| %>
    Admin?  : <%= permission_fields.check_box :admin %>
  <% end %>
<% end %>

Notice form_for @person and fields_for @person.permission - fields_for should be used for an association of a model, form_for should be used for the model itself. The reason your html is rendering as user_profile[user_profile] is that you're specifying @user_profile in both form_for and fields_for.

You likely need to change the parameter in form_for to @user (or the equivalent variable you have in scope). This will make your output html:

user[user_profile][first_name]

which you can pass to UsersController#update as long as User accepts_nested_attributes_for user_profile. If you want to instead pass the parameter hash from the form to UserProfilesController#update, then you want to drop the fields_for entirely and just render the fields under form_for @user_profile - which will give you html like user_profile[first_name].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文