ruby-on-rails – Rails accepted_nested_attributes_for错误,请帮我发现它

我一直在努力追随 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控制器……

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...