モデル:
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
エラーメッセージ:
ActiveRecord::AssociationTypeMismatch in Admin/dealersController#create User(#41048900) が予想され、取得した HashWithIndifferentAccess(#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" ハッシュを User オブジェクトに変換しないことだと思います。