【发布时间】:2011-10-28 22:27:21
【问题描述】:
我有一个 has_many 关系的嵌套表单,使用 Accepts_nested_attributes_for 方法。如果我没有子对象,那么嵌套表单将不会显示在父表单中。因此,我需要初始化子对象。一种方法是:
# app/helpers/form_helper.rb
module FormHelper
def setup_user(user)
3.times { user.tasks.build }
user
end
# app/views/users/_form.html.erb
<%= form_for(setup_user(user)) do |f| %>
...
如何将这种方法推广到其他嵌套形式?这是我想出的,但它不起作用
def initialize_children(parent, children)
parent.childrens ||= children.class.new
1.times { parent.childrens.build }
parent
end
谢谢
【问题讨论】:
标签: ruby-on-rails forms nested-forms