Note that there are some explanatory texts on larger screens.

plurals
  1. POid not set on nested form
    primarykey
    data
    text
    <p>I have 2 models Order and OrderItem. On Order#new I nested a new OrderItem. When I create the order it gets saved but not the OrderItem. Looks like the order_id is not set in OrderItem.</p> <pre><code>Order has_many :order_items accepts_nested_attributes_for :order_items, :reject_if =&gt; lambda { |a| a[:product_id].blank? } attr_accessible :id, :date, :note, :client_id, :order_items_attributes </code></pre> <p>In OrderItem</p> <pre><code>belongs_to :order attr_accessible :product_id, :order_id, :quantity, :color validates_presence_of :product_id, :order_id, :quantity </code></pre> <p>In my View</p> <pre><code>&lt;%= nested_form_for @order do |f| %&gt; &lt;%= f.fields_for :order_items do |item| %&gt; &lt;%= item.label :product_id %&gt; &lt;%= collection_select(:order_item, :product_id, Product.all, :id, :name, :prompt =&gt; 'Select Product') %&gt;&lt;br/&gt; &lt;%= item.label :quantity %&gt; &lt;%= item.number_field :quantity, :value =&gt; '1', :min =&gt; '1' %&gt;&lt;br /&gt; &lt;%= item.label :color_id %&gt; &lt;%= collection_select(:order_item, :color_id, Color.all, :id, :name, :prompt =&gt; 'Select Color') %&gt; &lt;%= item.link_to_remove "Remove this item" %&gt; &lt;% end %&gt; &lt;p&gt;&lt;%= f.link_to_add "Add an item", :order_items %&gt;&lt;/p&gt; &lt;% end %&gt; </code></pre> <p>Server log returns <code>"order"=&gt;{"client_id"=&gt;"1", "note"=&gt;"efewfwe", "order_items_attributes"=&gt;{"0"=&gt;{"quantity"=&gt;"1", "_destroy"=&gt;"false"}}}, "commit"=&gt;"Create Order", "order_items"=&gt;{"order_id"=&gt;""}, "order_item"=&gt;{"product_id"=&gt;"12", "color_id"=&gt;"4"}}</code></p> <p>Not sure what the first zero means but order_id is nil</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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