4

我收到 AssociationTypeMismatch 错误,我不确定我在哪里犯了错误。我对 Rails 很陌生,所以我猜我犯了一些愚蠢的错误。我检查了我的语法并将其与Ruby on Rails 应用程序上的 AssociationTypeMismatch 错误进行了比较

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

这是我的模型

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_preference只会accepts_nested_attributes_for :preference自己工作。

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

更新 好的,所以我想我离得更近了。在 Rails 控制台中四处寻找,我想我需要在 UserController 中创建 Preference.new ,然后才能传入哈希。由于我不确定 build_preference 到底做了什么,所以我还没有运气。

我已经尝试添加@user.preference = Preference.new上面的构建首选项并更改f.field_for :preference为,f.field_for @user.preference但我仍然遇到同样的错误。

更新 2 对于遇到此问题的其他任何人,答案是更改f.field_for :preferencef.field_for :preference_attributes. 请参阅下面的评论zetetic

4

2 回答 2

3

它是:

attr_accessible :preference_attributes

并以您的形式:

<%= f.fields_for :preference_attributes do |builder| %>
...
于 2014-01-28T14:02:22.753 回答
1

试一把。

在您的User模型中,尝试添加:preference_attributes到您的attr_accessible行中。

class User < ActiveRecord::Base
  attr_accessible :email, :full_name, :password, :password_confirmation, :preference_attributes
  .. # rest of your code goes here
end
于 2013-02-02T00:56:28.213 回答