Rails应用中的AssociationTypeMismatch(对象预期,获得了HashWithIndifferentAccess) [英] AssociationTypeMismatch (Object expected, got HashWithIndifferentAccess) in Rails app

查看:88
本文介绍了Rails应用中的AssociationTypeMismatch(对象预期,获得了HashWithIndifferentAccess)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了AssociationTypeMismatch错误,并且不确定在哪里出错.我对Rails很陌生,所以我猜我在犯一些愚蠢的错误.我已经检查了语法并将其与在Ruby on Rails应用上的AssociationTypeMismatch错误

I'm getting a AssociationTypeMismatch Error and I'm not sure where I'm making a mistake. I'm pretty new to Rails so I'm guessing I'm making some silly mistake. I've checked my syntax and compared it against AssociationTypeMismatch Error on Ruby on Rails app

...但是我似乎仍然无法捕捉到错误.

... but I still can't seem to catch the error.

这是我的模特

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

这是我的控制器:

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

这是我的观点

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
  <div class="error_messages">
    <h2>There's 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 %>

我的参数

{"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"}

修改: 我得到的错误是Preference(#70124732528700) expected, got ActiveSupport::HashWithIndifferentAccess(#70124687315200).我的理解是@user.build_preferenceaccepts_nested_attributes_for :preference只能单独工作.

The error I get is Preference(#70124732528700) expected, got ActiveSupport::HashWithIndifferentAccess(#70124687315200). My understanding is that @user.build_preference and accepts_nested_attributes_for :preference would just work on its own.

我是否需要创建一个def preferences_attributes=? http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

Do I need to create a def preferences_attributes= as per? http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

更新 好吧,我想我离这儿越来越近了.在Rails控制台中四处寻找,我认为需要先在UserController中创建Preference.new,然后才能传递哈希值.由于我不确定build_preference到底能做什么,所以我还没有运气.

Update Okay so I think I'm getting a bit closer. Poking around in the rails console, I figured I need to create Preference.new inside the UserController before I can pass in the hash. Since I'm not sure sure what build_preference does exactly, I'm not having any luck yet.

我尝试在构建首选项上方添加@user.preference = Preference.new并将f.field_for :preference更改为f.field_for @user.preference,但是我仍然遇到相同的错误.

I've tried adding @user.preference = Preference.new above build preference and changing f.field_for :preference to f.field_for @user.preference but I'm still getting the same error.

更新2 对于陷在此问题上的其他人,答案是将f.field_for :preference更改为f.field_for :preference_attributes.参见 zetetic 下方的评论.

Update 2 For anyone else that's stuck on this problem, the answer is to change f.field_for :preference to f.field_for :preference_attributes. See comment below by zetetic.

推荐答案

它是:

attr_accessible :preference_attributes

以及您的表单:

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

这篇关于Rails应用中的AssociationTypeMismatch(对象预期,获得了HashWithIndifferentAccess)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆