2010-11-07 67 views
6

我一直在试图关注Active Record Nested Attributes Guide,没有太大的成功。rails accept_nested_attributes_for错误,请帮我看看吧

我有以下型号:

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: Telephone(#80827590) expected, got Hash(#72886250)

能否请你帮我当场错误? 我应该在contact_controller.rb中包含哪些代码?

回答

10

我把它用下面的代码工作:

params = { :contact => { 
    :name => 'Joe', 
    :permanentcomment => "No Comment", 
    :telephones_attributes => [ 
     {:telephone => '787445741'}, 
     {:telephone => '478589658'} 
    ] 
    }} 
    Contact.create(params[:contact]) 

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