为什么<输入>是元素 ID 以嵌套形式重复?输入>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 fields_for 的文档中可以看出:
用一个显示此范例的示例:
注意
form_for @person
和fields_for @person.permission
- fields_for 应该用于模型的关联,form_for 应该用于模型本身。您的 html 呈现为user_profile[user_profile]
的原因是您在form_for
和fields_for@user_profile
代码>.您可能需要将
form_for
中的参数更改为 @user (或范围内的等效变量)。这将使您的输出为 html:只要用户
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:
With an example showing this paradigm:
Notice
form_for @person
andfields_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 asuser_profile[user_profile]
is that you're specifying@user_profile
in bothform_for
andfields_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: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 thefields_for
entirely and just render the fields underform_for @user_profile
- which will give you html likeuser_profile[first_name]
.