为什么在创建模型对象时会出现 AssociationTypeMismatch?

发布于 2024-08-29 11:28:56 字数 2413 浏览 6 评论 0原文

我收到以下错误:

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 < ActiveRecord::Base
  belongs_to :denccy, :class_name=>"Currency"
  belongs_to :numccy, :class_name=>"Currency"
  belongs_to :contract
end

我的视图是:

<% form_for @contract do |contractForm| %>


    Username: <%= contractForm.collection_select(:user_id, User.all, :id, :username) %> <br>


    B/S: <%= contractForm.select(:side,options_for_select([['BUY', 'BUY'], ['SELL', 'SELL']], 'BUY')) %> <br>


    Currency: <%= contractForm.collection_select(:currency_id, Currency.all, :id, :ccy) %> <br> <br>


    Amount: <%= contractForm.text_field :amount %> <br>

    <% contractForm.fields_for @contract.exchange_rate do |rateForm|%>
        Rate: <%= rateForm.text_field :rate %> <br>
    <% end %>

    <%= submit_tag :Create %>

<% end %>

我的视图控制器:

class ContractsController < ApplicationController

  def new
    @contract = Contract.new
    @contract.build_exchange_rate


    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @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

我不确定为什么它不识别汇率属性?

谢谢

I get the following error:

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"}}}

My relevant model is :

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 < ActiveRecord::Base
  belongs_to :denccy, :class_name=>"Currency"
  belongs_to :numccy, :class_name=>"Currency"
  belongs_to :contract
end

My view is:

<% form_for @contract do |contractForm| %>


    Username: <%= contractForm.collection_select(:user_id, User.all, :id, :username) %> <br>


    B/S: <%= contractForm.select(:side,options_for_select([['BUY', 'BUY'], ['SELL', 'SELL']], 'BUY')) %> <br>


    Currency: <%= contractForm.collection_select(:currency_id, Currency.all, :id, :ccy) %> <br> <br>


    Amount: <%= contractForm.text_field :amount %> <br>

    <% contractForm.fields_for @contract.exchange_rate do |rateForm|%>
        Rate: <%= rateForm.text_field :rate %> <br>
    <% end %>

    <%= submit_tag :Create %>

<% end %>

My View Controller:

class ContractsController < ApplicationController

  def new
    @contract = Contract.new
    @contract.build_exchange_rate


    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @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

I'm not sure why it's not recognizing the exchange rate attributes?

Thank you

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

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

发布评论

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

评论(1

微凉徒眸意 2024-09-05 11:28:56

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

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

The problem is that accepts_nested_attributes_for :exchange_rate looks for "exchange_rate_attributes" in the params, not "exchange_rate". The fields_for helper will do this for you, but you have to change it to:

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