5

ich eine verschachtelte Form haben mit einem has_and_belongs_to_many (Ryan B nested_form gem verwenden) Setup has_and_belongs_to_many:einen neuen Rekord in Rails HABTM verschachtelten Form Erstellen

Opening has_and_belongs_to_many :contacts

Contact has_and_belongs_to_many :openings

Beim Versuch, einen neuen Kontakt zu einer Öffnung hinzufügen möchten, in diesem Fall erhalte ich:

Can't mass-assign protected attributes: new_1346666966632

für

"opening"=>{"contacts_attributes"=>{"new_1346666966632"=>{"contacts"=>{"name"=>"Test Contact",

Ich habe die entsprechende "accepts_nested_attributes_for" und "attr_accessible", und am Aufbau der Kontakt dh @ opening.contacts.build und @ opening.contacts.build (params hinzugefügt [: Öffnen] [: contact_attributes]) in der Steuerung.

Wohin gehe ich falsch? Wäre es besser, hier eine has_many-through-Beziehung zu verwenden?

EDIT:

Ausblick:

<%= simple_nested_form_for @opening, :wrapper => :plain do |f| %> 
    <%= f.link_to_add "Add a contact", :contacts %> 
    <%= f.button :submit %> 
<% end %> 

die eine teilweise zu erzeugen Felder für verschachtelte Kontakt verwendet:

<%= f.fields_for :contacts, @opening.contacts.build do |contact_form| %> 
    <%= contact_form.input :name, :label => false, :input_html => { :class => 'span6' } %> 
    <%= contact_form.input :company, :label => false, :input_html => { :class => 'span6' } %> 
    <%= contact_form.input :telephone, :label => false, :input_html => { :class => 'span6' } %> 
    <%= contact_form.input :email_address, :label => false, :input_html => { :class => 'spa12' } %> 
<% end %> 
+0

versuchen hinzufügen attr_accessible: contacts_attributes und überprüfen – Amar

+0

sorry, ich vergaß zu erwähnen, schon habe, dass zu –

+0

Fügen Sie Ihre 'view' Codes auch – Samiron

Antwort

2

Sie müssen die Kontakte von der zu bauen/erstellen Öffnen des Modells, anstatt zu versuchen, die contacts_attributes manuell zuzuordnen. Ihr Controller-Code muss, wie etwas aussehen:

@opening.update_attributes(params[:opening]) 

Schauen Sie sich die Rails guide for more info on using nested attributes