Working As Intended

Rails accepts_nested_attributes_for and fields_for

July 1st 2010

I ran into a gotcha today while trying to combine accepts_nested_attributes_for in my model and fields_for in my view (via formtastic). The symptom was that none of my nested fields were showing up at all. The root cause was that the parent model didn’t have an instance of the child model.

The solution was to make sure that I added @parent.build_child in my controller’s edit and new actions to make sure that the associated model wasn’t nil.

blog comments powered by Disqus

Feed Feed