gpt4 book ai didi

ruby-on-rails - Rails Accepts_nested_attributes_for 错误,请帮我找出来

转载 作者:行者123 更新时间:2023-12-03 00:47:04 24 4
gpt4 key购买 nike

我一直在尝试遵循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 Controller 传递了错误的参数...

关于ruby-on-rails - Rails Accepts_nested_attributes_for 错误,请帮我找出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4118906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com