fields_for form builder object is nil

你。 提交于 2019-12-04 22:13:37

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 requires that the method tasks_attributes= exists. accepts_nested_attributes_for :tasks creates this method for you, but you can also just define it yourself:

def tasks_attributes=(params)
  # ... manually apply attributes in params to tasks
end

When this method does not exist, the builder.object ends up being nil.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!