Note that there are some explanatory texts on larger screens.

plurals
  1. POaccepts_nested_attributes not saving any changes
    primarykey
    data
    text
    <p>Maybe I'm missing something obvious (hopefully), but I'm encountering a weird problem saving records in a nested form. It's a pretty basic setup, with a minor complication in that my LineItem model is a two-word relationship (:line_items). However, I've followed Rails guidelines and it seems to be working OK.</p> <p>My fixtures are creating the proper relationships between line_items and invoices, and everything is showing up properly in my views, but I can't get any line_item records to save correctly (in my Rails console or my views).</p> <pre><code>class Invoice &lt; ActiveRecord::Base attr_accessible :line_items #and the rest of my relevant attributes has_many :line_items, :dependent =&gt; :destroy accepts_nested_attributes_for :line_items, :allow_destroy =&gt; true # Rest of my model code end class LineItem &lt; ActiveRecord::Base attr_accessible :invoice_id #and the rest of my relevant attributes belongs_to :invoice end </code></pre> <p>The <code>line_items_attributes=</code> method exists for my Invoices, but it doesn't save any line_items for new invoices. More irritating, I can edit existing line_items or assign them after the fact, but not in one fell swoop (the whole point of nested attributes?). My views can't even edit existing line_items through the invoice form. Any ideas? Happy to post more code, but didn't for sake of brevity.</p> <p>Thanks in advance...</p> <p>VIEW CODE (by request):</p> <p>(Form Partial for Invoices)</p> <pre><code>&lt;%= form_for(@invoice) do |f| %&gt; &lt;% @invoice.line_items.build unless @invoice.line_items.any? %&gt; ... &lt;% f.fields_for :line_items do |builder| %&gt; &lt;%= render 'line_item_fields', :f =&gt; builder %&gt; &lt;% end %&gt; </code></pre> <p>(Form Partial for Line Items)</p> <pre><code>... &lt;%= f.collection_select :sku_id, @skus, :id, :name, :prompt =&gt; true %&gt; &lt;%= f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") %&gt; </code></pre> <p>(javascript)</p> <pre><code>function remove_fields(link) { $(link).previous("input[type=hidden]").value = "1"; $(link).up(".fields").hide(); } </code></pre>
    singulars
    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.
 

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