2010-11-07 6 views
6

Ich habe versucht, die Active Record Nested Attributes Guide zu folgen, ohne viel Erfolg.Rails access_nested_attributes_for Fehler, bitte helfen Sie mir es zu finden

Ich habe die folgenden Modelle:

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

class Telephone < ActiveRecord::Base 
    belongs_to :contact 
end 

Wenn Sie einen Kontakt zu erstellen versuchen:

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

ich die folgende Fehlermeldung erhalten: ActiveRecord::AssociationTypeMismatch: Telephone(#80827590) expected, got Hash(#72886250)

Könnten Sie mir bitte helfen, die vor Ort Error? Gibt es einen Code, den ich in die contact_controller.rb aufnehmen sollte?

Antwort

10

Ich habe es mit dem folgenden Code arbeiten:

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

ich die falschen Argumente an die Contact.create Controller vorging ...

Verwandte Themen