# Rails Nested Object Form \*\_attributes problem

**URL:** <https://www.ruby-forum.com/t/rails-nested-object-form-attributes-problem/165365>\
**Category:** Rails\
**Created:** [April 13, 2009, 5:59am UTC](https://www.ruby-forum.com/t/rails-nested-object-form-attributes-problem/165365 "2009-04-13T05:59:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmrolfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@dmrolfs](https://www.ruby-forum.com/u/dmrolfs)\
**Post date:** [April 13, 2009, 5:59am UTC](https://www.ruby-forum.com/t/rails-nested-object-form-attributes-problem/165365/1 "2009-04-13T05:59:35Z")

</div>

I’m using Rails 2.3.2, and trying to get a nested object form to work  
properly. I’ve narrowed my problem to the issue that Rails is not  
setting my nested form elements with the \*\_attributes required to  
initiate the accepts\_nested\_attributes\_for processing. So instead of  
params being:

{“person”=\>{“name\_attributes”=\>{“given\_name”=\>“Fred”,  
“family\_name”=\>“Flintstone”}}, …}

I get:

{“person”=\>{“name”=\>{“given\_name”=\>“Fred”,  
“family\_name”=\>“Flintstone”}}, …}

As a result, I get a TypeMismatch exception. I’ve followed the  
documentation from Ryan Daigle. I’ve also followed the advice from this  
blog and the complex-forms-example.

Using Firebug, I went through my form and adjusted the name attribute of  
the input tags from name to name\_attributes. This produced the params  
with name\_attributes, and the create worked fine.

I’m stuck as I cannot figure out why my form is not producing the  
\*\_attributes form of the name.

Another thing I tried is I got the complex\_form\_example working in my  
environment. I’ve gone through every inch of the controller, models and  
views and compared it to my code. I cannot find what is different. I  
know this is something small, and would appreciate any help!

Thanks!

---

<div class="post-metadata">

**Author:** ![dmrolfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@dmrolfs](https://www.ruby-forum.com/u/dmrolfs)\
**Post date:** [April 13, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/rails-nested-object-form-attributes-problem/165365/2 "2009-04-13T20:51:26Z")

</div>

My model code is:

class Person \< Party  
has\_one :name, :class\_name =\> “PersonName”  
accepts\_nested\_attributes\_for :name, :allow\_destroy =\> true  
end

class PersonName \< ActiveRecord::Base  
belongs\_to :person  
end

My view code looks like this (I’m using HAML):

%h3 New customer  
= error\_messages\_for :person, :person\_name, :name, :country

- form\_for :person, :url =\> collection\_url, :html =\> {:class =\>  
‘MainForm’} do |person\_form|

= link\_to ‘Back’, collection\_url

---

<div class="post-metadata">

**Author:** ![dmrolfs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@dmrolfs](https://www.ruby-forum.com/u/dmrolfs)\
**Post date:** [April 14, 2009, 12:07am UTC](https://www.ruby-forum.com/t/rails-nested-object-form-attributes-problem/165365/3 "2009-04-14T00:07:48Z")

</div>

On Apr 13, 7:51 pm, Damon R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> My view code looks like this (I’m using HAML):
> 
> %h3 New customer  
> = error\_messages\_for :person, :person\_name, :name, :country
> 
> - form\_for :person, :url =\> collection\_url, :html =\> {:class =\>  
> ‘MainForm’} do |person\_form|

Hi Damon, this is quite an unusual error as the ‘\_attributes’ suffix  
is hard coded to be appended when the record object responds to symbol

- ‘\_attributes=’ (where symbol in this case is ‘name’). I would check  
to make sure that person\_form.object is definitely the @person object.  
Especially as it looks like you are setting @name in the controller  
for the error\_messages\_for. In fact I think posting the controller  
code would be quite useful if you’re still stuck.

Best regards,  
Andrew
