# Nested Forms

**URL:** <https://www.ruby-forum.com/t/nested-forms/171395>\
**Category:** Rails\
**Created:** [July 1, 2009, 6:23pm UTC](https://www.ruby-forum.com/t/nested-forms/171395 "2009-07-01T18:23:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 1, 2009, 6:23pm UTC](https://www.ruby-forum.com/t/nested-forms/171395/1 "2009-07-01T18:23:09Z")

</div>

I am using a nested form ( User has\_one Account … to hold profile  
data) to UPDATE my user/account data

User model  
has\_one :account  
accepts\_nested\_attributes\_for :account, :allow\_destroy =\> true

Account model  
belongs\_to :user

* * *

my form seems to work fine, I can see in the console, the @user  
attributes and the account attribute in the hash  
… “password”=\>"", account"=\>{“city”=\>"", “zip”=\>"",  
“country”=\>“BE”, “street\_address”=\>"", …}…

when I debug in the console,  
@user.attributes = params[:user]  
@user.valid? =\> true # OK no errors  
@user.attributes = params[:account]  
@account.valid? =\> false # OK , name is blank…

but if I debug my :edit action,  
if @user.update\_attributes(params[:user])  
I get true… which seems to mean that the account errors are not  
tracked ? according to the doc it should be false …

where am I wrong ?

additional Question : when errors , how should I display the them  
when they are related to the user.account  
should I use @user and @account and :  
error\_message\_on @account, :first\_name,

or only @user and  
error\_message\_on @user.account, :first\_name,

thanks for your recommandations

erwin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 1, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/nested-forms/171395/2 "2009-07-01T18:57:38Z")

</div>

The account data must be in :user =\> {:account\_attributes =\> {…}} if  
you want auto save it.

You can read about it in the documentation of  
ActiveRecord::NestedAttributes::ClassMethods (see fields\_for in  
ActionView::Helpers::FormHelper too).

Regards.

Franco C…

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 1, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/nested-forms/171395/3 "2009-07-01T18:59:58Z")

</div>

thanks Franco, … I was hitching my head and just discover in the  
log :

WARNING: Can’t mass-assign these protected attributes: account

so this attribute was wrong… and you gave me the answer !!
