Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 2.3 nested attributes issue
    text
    copied!<p>I'm running into to a issue with a nested form which I'm sure should be easily solved, yet I can't find a way around it</p> <p>Basically I have the following relationship</p> <pre><code>event has_many :contacts =&gt; through :event_contacts </code></pre> <p>the nested form works perfectly as long as I'm creating new contacts each time. </p> <p>If I include a drop down to allow selection of an existing contact within the nested form, an event is created, however the contact_id is nil in the event_contacts table.</p> <p>No contact is created, since I post an 'id' field for the nested contact.. (ie .new_record? returns false.)</p> <p>So how do I update the event_contacts table to ensure that the 'selected' contacts is properly associated?</p> <p>See snipbits below...</p> <p><strong>Models</strong></p> <pre><code>class Event &lt; ActiveRecord::Base belongs_to :user has_many :event_contacts accepts_nested_attributes_for :event_contacts has_many :contacts, :through =&gt; :event_contacts accepts_nested_attributes_for :contacts class EventContact &lt; ActiveRecord::Base belongs_to :event belongs_to :contact accepts_nested_attributes_for :contact class Contact &lt; ActiveRecord::Base belongs_to :user has_many :event _contacts has_many :events, :through =&gt; :event_contacts </code></pre> <p><strong>View</strong></p> <pre><code>- fields_for(@event) do |f| - f.fields_for :event_contacts do |rc| - rc.fields_for :contact do |c| = c.collection_select :id, Contact.all, :id, :name, { :selected =&gt; @contact.id || 0 } = c.hidden_field :user_id, :value =&gt; @current_user.id = c.text_field :first_name = c.text_field :email </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload