Re: Re: Help needed understanding "fields_for" and resultant controller code

Jim Burgess
Wed, 29 Dec 2010 21:25:45 -0000

Hi Bente,
Thanks for your reply.

> You should use
> accepts_nested_attributes_for :department
> in your employee model.

The code you provided needed a minor tweak:

<% form.fields_for @employee.department do|department_fields|  %>
was throwing an ActiveRecord::AssociationTypeMismatch -
Department(#77492292) expected, got HashWithIndifferentAccess(#37082688)

After a bit of googling  I changed it to:
<% form.fields_for :department do|department_fields|  %>

and everything worked fine.

Thank you once again.

P.S. In case it helps anyone else, this is described very well here: