正确实例化嵌套 fields_for 而不显示较旧的保存对象?
我有一个嵌套表单,实例化如下:
- 2.times { @organization.referrals.build }
- form_for @organization do |f|
= f.error_messages
- f.fields_for :referrals do |f|
除了,嵌套表单应该始终是新的和唯一的。这种形式也显示了以前创建的对象。
所以我尝试这样写..
- 2.times { @organization.referrals.build }
- form_for @organization do |f|
= f.error_messages
- f.fields_for @organization.referrals.select{|r| r.new_record? } do |f|
但现在我没有看到 2 个空白表单,并且我无法保存我的对象,因为它尝试传递 Organization.referral
(这不是一种方法)而不是Organization.referrals
。
问题 1
如何创建 2 个空白表单?
问题 2
我如何才能正确通过(如第一个示例中所示)?
I have a nested form that instantiates as this :
- 2.times { @organization.referrals.build }
- form_for @organization do |f|
= f.error_messages
- f.fields_for :referrals do |f|
Except, the nested forms are supposed to be always new and unique. Where as this form shows previously created objects as well.
So I tried to write as so..
- 2.times { @organization.referrals.build }
- form_for @organization do |f|
= f.error_messages
- f.fields_for @organization.referrals.select{|r| r.new_record? } do |f|
But now I do not see 2 blank forms, and I can not save my object because it tries to pass Organization.referral
(which is not a method) instead of Organization.referrals
.
Question 1
How do I create 2 blank forms?
Question 2
How do I get this to pass correctly ( as it does in the first example ) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
模型
视图
Try this:
model
view