fields_for 表单生成器对象为 nil
有什么方法可以访问嵌套的 form_bulder.object 吗?
## controller
@project = Project.new
@project.tasks.build
form_for(@project) do |f|
f.object.nil? ## returns false
fields_for :tasks do |builder|
builder.object.nil? ## returns true
end
end
Any way to access a nested form_bulder.object?
## controller
@project = Project.new
@project.tasks.build
form_for(@project) do |f|
f.object.nil? ## returns false
fields_for :tasks do |builder|
builder.object.nil? ## returns true
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在项目模型中具有 Accepts_nested_attributes_for 才能传递对象。
You must have accepts_nested_attributes_for in the Project model in order for the object to be passed.
fields_for
要求tasks_attributes=
方法存在。accepts_nested_attributes_for :tasks
为您创建此方法,但您也可以自己定义它:当此方法不存在时,
builder.object
最终为零。fields_for
requires that the methodtasks_attributes=
exists.accepts_nested_attributes_for :tasks
creates this method for you, but you can also just define it yourself:When this method does not exist, the
builder.object
ends up being nil.