Rails -- fields_for 不起作用?
当我运行 Rails 服务器并转到网站上的学校/新页面时,会出现带有标签“学校”的字段,我可以在其中输入学校名称,但 fields_for 下的所有其他字段用于输入学校管理员的信息不会出现在我的网站上——当我在表单上使用“检查元素”时,它们就好像不存在一样。为什么它们没有出现在我的页面上?
<%= form_for(@school) do |f| %>
<% if @school.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@school.errors.count, "error") %> prohibited this school from being saved:</h2>
<ul>
<% @school.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :school %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :admin do |f2| %>
<div class="field">
<%= f2.label "administrator name" %><br />
<%= f2.text_field :name %>
</div>
<div class="field">
<%= f2.label "administrator email" %><br />
<%= f2.text_field :email %>
</div>
<div class="field">
<%= f2.label "administrator gender" %><br />
<%= f2.text_field :gender %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
When I run the rails server and go to the school/new page on my site, the field with the label "school" where I can enter the school's name appears, but all the other fields under fields_for which are for entering the school administrator's info do not show up on my site -- when I use "inspect element" on my form it is like they are not even there. Why aren't they appearing on my page?
<%= form_for(@school) do |f| %>
<% if @school.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@school.errors.count, "error") %> prohibited this school from being saved:</h2>
<ul>
<% @school.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :school %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :admin do |f2| %>
<div class="field">
<%= f2.label "administrator name" %><br />
<%= f2.text_field :name %>
</div>
<div class="field">
<%= f2.label "administrator email" %><br />
<%= f2.text_field :email %>
</div>
<div class="field">
<%= f2.label "administrator gender" %><br />
<%= f2.text_field :gender %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TLDR:
出于我的目的,我必须确保表单对象的子关联已填充新记录。最初,父对象的子关联返回 nil。我只需要实例化一个对象并且表单就可以正确呈现。
如果有人从谷歌到达这里,我遇到了与海报相同的问题,但接受的答案对我没有帮助。
首先,一些背景知识。当您使用普通的
fields_for
而不是f.fields_for
时,字段的呈现方式会有所不同。普通版本(如上所述)不会将呈现的字段与表单的对象相关联。查看不同的输出:fields_for
Renders
哪些 Rails 将解析为这些参数:
f.fields_for
Renders
哪些 Rails 将解析为这些参数:
区别在于 当您使用
时,子对象的参数仅嵌套在父对象下>f.fields_for
。 因此,使用已接受答案的建议(使用裸fields_for
)无法创建关联记录。事实证明,
f.fields_for
没有渲染任何内容的原因是子关联为零。为了修复它并让我的表单正确渲染并以正确嵌套的方式返回参数,我必须在控制器中实例化该关联,如下所示:TLDR:
For my purposes, I had to make sure the form object's child association was populated with a new record. Originally, the parent object's child association returned nil. I just had to instantiate an object and the form rendered correctly.
In case anyone arrives here from Google, I had a the same problem as the poster, but the accepted answer didn't help me.
First, some background. When you use the plain
fields_for
instead off.fields_for
, the fields are rendered differently. The plain version (as suggested above) does not associate the fields rendered with the form's object. Look at the different outputs:fields_for
Renders
Which Rails will parse into these parameters:
f.fields_for
Renders
Which Rails will parse into these parameters:
The difference is that the child object's parameters only get nested under the parent when you use
f.fields_for
. So using the accepted answer's suggestion of using the barefields_for
didn't work for creating an associated record.Turned out, the reason
f.fields_for
wasn't rendering anything was because the child association was nil. To fix it and have my form render correctly and return the parameters in the properly nested way, I had to instantiate that association like this in my controller:尝试删除 f。
尝试这样做:
有两种使用 field_for 的方法:
一种通用方法。 (就像我的例子)。
一个嵌套的。
要使用嵌套的 field_for (f.field_for),您的模型对象必须正确设置
例如:与 @school 和 @admin 的关系
最后,有多种使用 field_for 的方法,具体取决于您如何在模型对象上设置关系。
本指南详细解释了所有可能的设置。您可能会发现为什么它不适合您。
我敢打赌,您的 Admin 和 School 模型对象不存在关系,或者您没有将 Accept_nested_attributes_for :admin 放入您的 School 对象中。
try removing the f.
try with just this:
There is two way to use field_for:
A generic one. (like my example).
A nested one.
To use the nested field_for (f.field_for), your model object must be setup correctly
Ex: Relation with @school and @admin
In the end, there is multiple ways of using field_for depending on how you setup the relations on your model object.
This guide explain in details all the possible setup. You might find why it was not working for you.
My bet is your Admin and School model object are not in a relation, or you didn't put the accepts_nested_attributes_for :admin in your School object.