AssociationTypeMismatch (object expected, got HashWithIndifferentAccess) in Rails app

I am getting an AssociationTypeMismatch error and I am not sure where I am making the error. I'm new to Rails, so I'm guessing I'm making some silly mistake. I checked my syntax and compared it to Association Error TypeMismatch in Ruby on Rails Application

... but I still can't figure out the error.

Here are my models

class User < ActiveRecord::Base
  attr_accessible :email, :full_name, :password, :password_confirmation, :preference
  has_secure_password

  validates_uniqueness_of :email
  validates_presence_of :full_name

  has_one :preference, :dependent => :destroy

  accepts_nested_attributes_for :preference
end

class Preference < ActiveRecord::Base
  attr_accessible :background_fill, :background_position, :layout

  belongs_to :user
end

      

Here's my controller:

def new
  @user = User.new
  @user.build_preference
end

def create
  @user = User.new(params[:user])
  if @user.save
    session[:user_id] = @user.id
    redirect_to root_url, notice: "Thanks for signing up!"
  else
    render "new"
  end
end

      

Here's my view:

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
  <div class="error_messages">
    <h2>There an error!</h2>
    <ul>
      <% @user.errors.full_message.each do |message| %>
        <li><%= message %></li>
      <% end %>
    </ul>
  </div>
  <% end %>
  <%= f.label :full_name %>
  <%= f.text_field :full_name, :class => "target", :placeholder => "Your full name", :maxlength => "55", :autofocus => "autofocus" %>
  <%= f.label :email %>
  <%= f.email_field :email, :class => "target", :placeholder => "example@gmail.com", :maxlength => "55" %>
  <%= f.label :password %>
  <%= f.password_field :password, :class => "target", :placeholder => "Enter a password", :maxlength => "55" %>
  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation, :class => "target", :placeholder => "Enter your password again", :maxlength => "55" %>
  <%= f.fields_for :preference do |builder| %>
    <%= builder.hidden_field :layout, value: params[:layout] %>
    <%= builder.hidden_field :background_fill, value: params[:background_fill] %>
    <%= builder.hidden_field :background_position, value: params[:background_position] %>
  <% end %>
  <%= f.submit "Create an Account", :class => "button cta" %>
<% end %>

      

And my parameters

{"utf8"=>"โœ“",
"authenticity_token"=>"[redacted]=",
"user"=>{"full_name"=>"test",
"email"=>"test@example.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"preference"=>{"layout"=>"layout-3",
"background_fill"=>"restaurant-2",
"background_position"=>"position-center"}},
"commit"=>"Create an Account"}

      

Edit: The error I am getting is Preference(#70124732528700) expected, got ActiveSupport::HashWithIndifferentAccess(#70124687315200)

. I understand that @user.build_preference

they accepts_nested_attributes_for :preference

will work independently.

Do I need to create def preferences_attributes=

according to? http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

Update Ok, so I think I'm a little closer. After diving into the rails console, I decided that I need to create a Preference.new inside the UserController before I can step into the hash. Since I'm not sure what exactly build_preference does, I have no luck yet.

I tried to add the @user.preference = Preference.new

above build settings and change f.field_for :preference

to f.field_for @user.preference

, but I still get the same error.

Update 2 For anyone stuck with this issue, the answer is to change f.field_for :preference

to f.field_for :preference_attributes

. See comment below zetetic .

+3


source to share


2 answers


this is:

attr_accessible :preference_attributes

      



and in your form:

<%= f.fields_for :preference_attributes do |builder| %>
...

      

+3


source


Take a picture.

In your model, User

try adding :preference_attributes

to your string attr_accessible

.



class User < ActiveRecord::Base
  attr_accessible :email, :full_name, :password, :password_confirmation, :preference_attributes
  .. # rest of your code goes here
end

      

+1


source







All Articles