多态嵌套形式导致 AssociationTypeMismatch

发布于 2024-08-06 03:42:49 字数 1710 浏览 4 评论 0原文

型号:

class User < ActiveRecord::Base
  belongs_to :role, :polymorphic => true

class Admin < ActiveRecord::Base
  has_one :user, :as => :role

class Dealer < ActiveRecord::Base
  has_one :user, :as => :role

class Buyer < ActiveRecord::Base
  has_one :user, :as => :role

经销商控制器:

  def new
    @dealer = Dealer.new
    respond_to do |format|
      format.html
      format.xml  { render :xml => @dealer }
    end
  end

  def create
    @dealer = Dealer.new(params[:dealer])
    respond_to do |format|
      if @dealer.save
        flash[:notice] = 'Dealer was successfully created.'
        format.html { redirect_to [:admin, @dealer] }
        format.xml  { render :xml => @dealer, :status => :created, :location => @dealer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @dealer.errors, :status => :unprocessable_entity }
      end
    end
  end

错误消息:

Admin/dealersController#create 中的 ActiveRecord::AssociationTypeMismatch 预期用户(#41048900),得到 HashWithIn DifferentAccess(#23699520)

请求参数:

{"authenticity_token"=>"+GkeOOxVi1Fxl7ccbV0Ctt5R6shyMlF+3UWgRow2RdI=",
 "dealer"=>{"gender"=>"m",
 "forename"=>"",
 "surname"=>"",
 "company"=>"",
 "address1"=>"",
 "address2"=>"",
 "zipcode"=>"",
 "city"=>"",
 "country"=>"1",
 "user"=>{"email"=>"",
 "password"=>""},
 "phone"=>"",
 "mobile"=>"",
 "fax"=>""},
 "commit"=>"Submit"}

我想我的问题是 Rails 不会将请求哈希内的“用户”哈希转换为 User 对象 - 但为什么以及如何使 Rails 能够这样做吗?

Models:

class User < ActiveRecord::Base
  belongs_to :role, :polymorphic => true

class Admin < ActiveRecord::Base
  has_one :user, :as => :role

class Dealer < ActiveRecord::Base
  has_one :user, :as => :role

class Buyer < ActiveRecord::Base
  has_one :user, :as => :role

Dealer controller:

  def new
    @dealer = Dealer.new
    respond_to do |format|
      format.html
      format.xml  { render :xml => @dealer }
    end
  end

  def create
    @dealer = Dealer.new(params[:dealer])
    respond_to do |format|
      if @dealer.save
        flash[:notice] = 'Dealer was successfully created.'
        format.html { redirect_to [:admin, @dealer] }
        format.xml  { render :xml => @dealer, :status => :created, :location => @dealer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @dealer.errors, :status => :unprocessable_entity }
      end
    end
  end

Error message:

ActiveRecord::AssociationTypeMismatch in Admin/dealersController#create
User(#41048900) expected, got HashWithIndifferentAccess(#23699520)

Request Parameters:

{"authenticity_token"=>"+GkeOOxVi1Fxl7ccbV0Ctt5R6shyMlF+3UWgRow2RdI=",
 "dealer"=>{"gender"=>"m",
 "forename"=>"",
 "surname"=>"",
 "company"=>"",
 "address1"=>"",
 "address2"=>"",
 "zipcode"=>"",
 "city"=>"",
 "country"=>"1",
 "user"=>{"email"=>"",
 "password"=>""},
 "phone"=>"",
 "mobile"=>"",
 "fax"=>""},
 "commit"=>"Submit"}

I guess my problem is that Rails doesn't convert the "user" hash inside the request hash into a User object — but why and how can I make Rails to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-08-13 03:42:49

刚刚花了几个小时在这上面,包括碰到这个线程。终于在另一个论坛找到了:
属性设置器期望以这种形式获取你的参数,

{ ...,
  "user_attributes" => {"email" => "", "password" => ""},
  ... }

而不是

{ ...,
  "user" => {"email" => "", "password" => ""},
  ... }

为了得到这个,你的视图应该看起来像这样:

form_for @dealer do |f|
 ...
 f.fields_for :user, @dealer.user do |u|
   ...
 end
 ...
end

这对我在 Rails 3.0.3 上有效

Just spent several hours on this including bumping into this thread. Finally found on another forum:
the attribute setter expects to get your params in this form

{ ...,
  "user_attributes" => {"email" => "", "password" => ""},
  ... }

NOT

{ ...,
  "user" => {"email" => "", "password" => ""},
  ... }

In order to get this, your view should look something like this:

form_for @dealer do |f|
 ...
 f.fields_for :user, @dealer.user do |u|
   ...
 end
 ...
end

This works for me on Rails 3.0.3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文