Im making nested attributes for profile on the user model and I also
need to validate the profile info
so I put
User model
has_one :profile
accepts_nested_attributes_for :profile
validates_associated :profile <— To validate values
attr_accessible :profile <----to allow mass assingment
Profile model
belongs_to :user, :dependent => :destroy
Some Validations <<<
But Im getting
Profile(#30752520) expected, got
ActiveSupport::HashWithIndifferentAccess(#26724528)
Becouse of the
attr_accessible :profile
I think, but if I dont have that line I cant enter values for profile,
Anyways. when I take that line off, my profile forms isn’t validated,
this problem come when changing to rails 3.0.7
Hi Tomas,
Try changing your mass-assignment protection line to:
attr_accessible :profile_attributes
If that still doesn’t work send your view code. If you haven’t
already ready read the nested attribute docs, they explain things
pretty well:
Matt Daubert
I added that line but still dont work
User Form
<%= form_for @user do |f| %>
<%= render ‘shared/error_messages’, :record => @user %>
Some Field Tags <<<
<%= f.fields_for @user.build_profile do |builder| %>
<%= render 'profile_form', :f => builder %>
<% end %>
<%= f.submit (@user.new_record? ? "Registrarse" : "Editar") %>
<% end %>
Profile Fields
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :surname %>
<%= f.text_field :surname %>
On User Model
has_one :profile
attr_accessible :username, :password, :profile_attributes
accepts_nested_attributes_for :profile
validates_associated :profile
When submiting
WARNING: Can’t mass-assign protected attributes: profile
Fixed: Changed @user.build_profile from view to controller