Nested_attributes

I have a user object, and a user_profile object.

In my form I have:

<% form_for(:user, :url => object_url, :html => { :method => :put })
do |f| %>
<% f.fields_for :user_profile do |profile_form| %>

  <div>
  <%= profile_form.label :first_name, t("first_name") %><br />
  <%= profile_form.text_field :first_name %>
  </div>

  <div>
  <%= profile_form.label :last_name, t("last_name") %><br />
  <%= profile_form.text_field :last_name %>
  </div>

<% end %>

In my model:

class User < ActiveRecord::Base
has_one :user_profile
accepts_nested_attributes_for :user_profile

attr_accessible :email, :password, :password_confirmation,
:user_profile, :user_profile_attributes

And my controller is using the resource_controller plugin.

When I go to do an update, I’m getting:

ActiveRecord::AssociationTypeMismatch (UserProfile(#2197780400)
expected, got HashWithIndifferentAccess(#2159358740))

Here’s my data:

Parameters: {“action”=>“update”, “_method”=>“put”,
“authenticity_token”=>“tkMl1SIlEf2s6wV60cAL/J7hOF2ql614cwWuI4da5lg=”,
“id”=>“1”, “controller”=>“admin/users”, “user”=>{“user_profile”=>
{“last_name”=>“asdf”, “first_name”=>“asdf”},
“password_confirmation”=>"[FILTERED]", “role”=>{“admin”=>“1”,
“user”=>“1”}, “password”=>"[FILTERED]", “email”=>“[email protected]”}}

On Oct 12, 4:27 am, Jeff McFadden [email protected] wrote:

  <%= profile_form.text_field :first_name %>

In my model:

“user”=>“1”}, “password”=>"[FILTERED]", “email”=>“[email protected]”}}
I think the parameter key for updating the user_profile relation
should be user_profile_attributes, I’m not sure why the fields_for
call is generating it without the suffix. Have you checked the bug
tracker? I would test it without the attr_accessible call as well.

Hope that helps,
Andrew

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs