Nested Forms

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

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…

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 !!