Rails的accepts

编程入门 行业动态 更新时间:2024-10-26 20:30:38
本文介绍了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

本文发布于:2023-10-18 03:10:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   accepts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!