多态嵌套形式导致 AssociationTypeMismatch
型号:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚花了几个小时在这上面,包括碰到这个线程。终于在另一个论坛找到了:
属性设置器期望以这种形式获取你的参数,
而不是
为了得到这个,你的视图应该看起来像这样:
这对我在 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
NOT
In order to get this, your view should look something like this:
This works for me on Rails 3.0.3