2010-11-07 22 views
6

Tôi đã cố gắng theo dõi Active Record Nested Attributes Guide, mà không thành công nhiều.Rails accept_nested_attributes_for Lỗi, hãy giúp tôi phát hiện ra nó

tôi có các mô hình sau:

class Contact < ActiveRecord::Base 
    has_many :telephones 
    accepts_nested_attributes_for :telephones 
end 

class Telephone < ActiveRecord::Base 
    belongs_to :contact 
end 

Khi cố gắng để tạo ra một số liên lạc:

contact = { 
    :name => "John", 
    :telephones => [ 
    {:telephone => '787445741'}, 
    {:telephone => '478589658'} 
    ] 
} 
Contact.create(contact) 

tôi nhận được lỗi sau: ActiveRecord::AssociationTypeMismatch: Telephone(#80827590) expected, got Hash(#72886250)

Ông có thể vui lòng giúp tôi nhận ra sự lỗi? Có mã nào tôi nên đưa vào trong contact_controller.rb không?

Trả lời

10

Tôi đã làm việc với đoạn mã sau:

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

Tôi đã đi qua những lập luận sai với bộ điều khiển Contact.create ...

Các vấn đề liên quan