加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails accepted_nested_attributes_for错误,请

发布时间:2020-12-16 23:03:50 所属栏目:百科 来源:网络整理
导读:我一直在努力追随 Active Record Nested Attributes Guide,没有太大的成功. 我有以下型号: class Contact ActiveRecord::Base has_many :telephones accepts_nested_attributes_for :telephonesendclass Telephone ActiveRecord::Base belongs_to :contacte
我一直在努力追随 Active Record Nested Attributes Guide,没有太大的成功.

我有以下型号:

class Contact < ActiveRecord::Base
  has_many :telephones
  accepts_nested_attributes_for :telephones
end

class Telephone < ActiveRecord::Base
  belongs_to :contact
end

在尝试创建联系人时:

contact = {
  :name => "John",:telephones => [
    {:telephone => '787445741'},{:telephone => '478589658'}
  ]
}
Contact.create(contact)

我收到以下错误:
ActiveRecord :: AssociationTypeMismatch:电话(#80827590)预计,得到哈希(#72886250)

你能帮我看看错误吗?
我应该在contact_controller.rb中包含任何代码吗?

解决方法

我使用以下代码:
params = { :contact => {
    :name => 'Joe',:permanentcomment => "No Comment",:telephones_attributes => [
      {:telephone => '787445741'},{:telephone => '478589658'}
    ]
  }}
  Contact.create(params[:contact])

我将错误的参数传递给Contact.create控制器……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读