为什么在创建模型对象时会获得AssociationTypeMismatch?

我收到以下错误:

ActiveRecord::AssociationTypeMismatch in ContractsController#create ExchangeRate(#2183081860) expected, got HashWithIndifferentAccess(#2159586480) Params: {"commit"=>"Create", "authenticity_token"=>"g2/Vm2pTcDGk6uRas+aTgpiQiGDY8lsc3UoL8iE+7+E=", "contract"=>{"side"=>"BUY", "currency_id"=>"488525179", "amount"=>"1000", "user_id"=>"633107804", "exchange_rate"=>{"rate"=>"1.7"}}} 

我的相关模型是:

 class Contract < ActiveRecord::Base belongs_to :currency belongs_to :user has_one :exchange_rate has_many :trades accepts_nested_attributes_for :exchange_rate end class ExchangeRate "Currency" belongs_to :numccy, :class_name=>"Currency" belongs_to :contract end 

我的观点是:

  Username:  
B/S:
Currency:

Amount:
Rate:

我的视图控制器:

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

我不确定为什么它不承认汇率属性?

谢谢

问题是accepts_nested_attributes_for :exchange_rate在参数中查找"exchange_rate_attributes" ,而不是"exchange_rate"fields_for帮助器将为您执行此操作,但您必须将其更改为:

 <% contractForm.fields_for :exchange_rate do |rateForm|%>