fields_for 表单构建器对象为零 [英] fields_for form builder object is nil
本文介绍了fields_for 表单构建器对象为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以访问嵌套的 form_bulder.object?
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
推荐答案
您必须在 Project 模型中有 accepts_nested_attributes_for 才能传递对象.
You must have accepts_nested_attributes_for in the Project model in order for the object to be passed.
class Project < ActiveRecord::Base
has_many :tasks
accepts_nested_attributes_for :tasks ## this is required
end
这篇关于fields_for 表单构建器对象为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文