使用collection.build时未创建belongs_to关联
我有这样的形式:
<%= form_for(@debate.debates.build) do |support_form| %>
<div>
<%= support_form.label :content %><br />
<%= support_form.text_area :content %>
</div>
<%= support_form.hidden_field :is_supporting, :value => is_supporting %>
<div class="actions">
<%= support_form.submit %>
</div>
<% end %>
每个辩论都有_许多辩论,并且属于一个辩论(树结构),并且@debate.debates.build应该创建一个新的辩论,它是@debate的子项,但是由@debate.debates创建的辩论.build 总是 nil
当我在 irb 中运行相同的代码时,关联已正确设置,并且新辩论的辩论 id 是其父级,这正是我希望的方式。
这是怎么回事?我如何确保新辩论的父辩论 ID 设置正确?
I have this form:
<%= form_for(@debate.debates.build) do |support_form| %>
<div>
<%= support_form.label :content %><br />
<%= support_form.text_area :content %>
</div>
<%= support_form.hidden_field :is_supporting, :value => is_supporting %>
<div class="actions">
<%= support_form.submit %>
</div>
<% end %>
Each debate has_many debate and belongs to a debate (a tree structure) and @debate.debates.build is supposed to create a new debate that is the child of @debate, but the debate created by @debate.debates.build is always nil
When I run the same code in irb, though, the association is correctly set up, and the debate id of the new debate is its parent, the way I want it to be.
Whats going on? And how can I make sure that the new debate has its parent debates id set up correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在内存中构建它,但我认为它实际上不会保存它,直到运行保存,大概是在表单的接收操作中,可能是控制器的创建操作。
为了使其发挥作用,辩论 ID(
debate_id
?您没有显示足够的模型)可能也需要采用表单。最后,该问题可能与
attr_accessible
或attr_protected
问题有关。如果不允许将辩论 ID 设置为批量属性更新的一部分,则它可能会在提交中丢失。It builds it in memory, but I don't think it actually saves it until a save is run, presumably in the recipient action of the form, probably the create action of the controller.
In order for that to work, the debate id (
debate_id
? you don't show enough of your model) may need to be in the form as well.Finally, the issue could be related to
attr_accessible
orattr_protected
issues. If the debate id is not allowed to be set as part of the bulk attribute update, it may get lost in the submission.