Rails的accepts_nested_attributes_for错误,请帮我发现它 [英] Rails accepts_nested_attributes_for Error, please help me spot it
本文介绍了Rails的accepts_nested_attributes_for错误,请帮我发现它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在努力遵循活动记录嵌套属性指南,但没有成功。
I have been trying to follow the Active Record Nested Attributes Guide, without much success.
我有以下型号:
class Contact < ActiveRecord::Base
has_many :telephones
accepts_nested_attributes_for :telephones
end
class Telephone < ActiveRecord::Base
belongs_to :contact
end
在试图创建一个联系人:
When trying to create a contact:
contact = {
:name => "John",
:telephones => [
{:telephone => '787445741'},
{:telephone => '478589658'}
]
}
Contact.create(contact)
我收到以下错误:
的ActiveRecord :: AssociationTypeMismatch:电话(#80827590)预计,得到了哈希(#72886250)
I get the following error:
ActiveRecord::AssociationTypeMismatch: Telephone(#80827590) expected, got Hash(#72886250)
能否请你帮我找到这个错误?
有没有code,我应该包括在 contact_controller.rb
?
Could you please help me spot the error?
Is there any code that I should include in the contact_controller.rb
?
推荐答案
我得到了它具有以下code工作:
I got it working with the following code:
params = { :contact => {
:name => 'Joe',
:permanentcomment => "No Comment",
:telephones_attributes => [
{:telephone => '787445741'},
{:telephone => '478589658'}
]
}}
Contact.create(params[:contact])
我经过了错误的参数给 Contact.create
控制器...
这篇关于Rails的accepts_nested_attributes_for错误,请帮我发现它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文