2010-11-07 41 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控制器...