以下模型:的ActiveRecord :: AssociationTypeMismatch {}這是哈希(#70364214688040)的一個實例,使用 「CLASS_NAME」
class Shipment < ApplicationRecord
belongs_to :origin, :class_name => "Location"
belongs_to :destination,:class_name => "Location"
end
在我的控制,我試圖建立一個新的實例:
@shipment = Shipment.new({
"origin" => {"name"=>"12312", "country"=>"US", "city"=>"Cambridge", "state"=>"MA", "postal_code"=>"02138", "address1"=>"Massachusetts Avenue, 1234", "address2"=>"123213"},
"destination" => {"name"=>"12312", "country"=>"US", "city"=>"Cambridge", "state"=>"MA", "postal_code"=>"02138", "address1"=>"Massachusetts Avenue, 1234", "address2"=>"123213"}})
以下PARAMS給我的錯誤
ActiveRecord::AssociationTypeMismatch: Location(#70364217448000) expected, got {"name"=>"12312", "country"=>"US", "city"=>"Cambridge", "state"=>"MA", "postal_code"=>"02138", "address1"=>"Massachusetts Avenue, 1234", "address2"=>"123213"} which is an instance of Hash(#70364214688040)
繞過這個,我用這:
@shipment = Shipment.new
@shipment.build_origin {"name"=>"12312", "country"=>"US", "city"=>"Cambridge", "state"=>"MA", "postal_code"=>"02138", "address1"=>"Massachusetts Avenue, 1234", "address2"=>"123213"}
@shipment.build_destination {"name"=>"12312", "country"=>"US", "city"=>"Cambridge", "state"=>"MA", "postal_code"=>"02138", "address1"=>"Massachusetts Avenue, 1234", "address2"=>"123213"}
於是,我試着在模型中包括以下內容:
accepts_nested_attributes_for :origin
accepts_nested_attributes_for :destination
但比我得到的失敗空白驗證。
如何修復模型以允許嵌套屬性?
你是最棒的!謝謝! – user664859