All Nested Attributes in rails form save with the value of the first one
<p>I have a problem with updating a nested attribute for an <code>invoice</code> using <code>accepts_nested_attributes_for</code> on my rails project:</p>

<p>When I select the quantity for each item they all save the same as the value for the first item, for example:</p>

<p>In the form for the invoice I have 3 different items with 3 different quantities for each of them. Independently of what value I select for each of them, the records are always saved with the value of the first item.</p>

<p>Here is the Controller:</p>

<pre><code>def edit
  @invoice = Invoice.find(params[:id])
  @printers = Printer.cheap_first
  (9-@invoice.invoice_items.size).times do
  @invoice.invoice_items.build
  end
end

def update
  @invoice = Invoice.find(params[:id])
  if @invoice.update(invoice_params) 
      flash[:notice] = "Invoice updated succesfully."
      redirect_to(:action =&gt;'show')
  else
    render('edit')
  end
end

private
def invoice_params
  params.require(:invoice).permit(:client, :salesman, :discount, :organization, :address, :zp, :currency, :rate, invoice_items_attributes: [:id, :quantity, :printer_id, :_destroy])
end
</code></pre>

<p>Here is the Model:</p>

<pre><code>class Invoice &lt; ActiveRecord::Base
  has_many :invoice_items, dependent: :destroy
  has_many :printers, :through =&gt; :invoice_items
  accepts_nested_attributes_for :invoice_items, reject_if: proc {|attributes| attributes['printer_id'].blank?}, allow_destroy: true
end
</code></pre>

<p>Here is the View:</p>

<pre><code>&lt;% z = 0 %&gt;
&lt;%= f.fields_for :invoice_items, remote: true do |ff| %&gt;
&lt;div id=&lt;%="invoice_item_#{z}"%&gt; class="hidden" &gt;
    &lt;div class="col-md-4 col-sm-6 col-xs-12"&gt;
    &lt;h3&gt;&lt;%="Printer #{z+1}"%&gt;&lt;/h3&gt;
    &lt;div class="form-group form-horizontal"&gt;
        &lt;%= ff.label(:printer_id, "Printer:") %&gt;
        &lt;div class="input-group"&gt;
            &lt;div class="input-group-addon"&gt;
                &lt;i class="fa fa-cube fa-lg" aria-hidden="true"&gt;&lt;/i&gt;
            &lt;/div&gt;
            &lt;%= ff.select(:printer_id, @printers.map { |s| [s.name, s.id] }, {:include_blank =&gt; 'Select a printer'},{:class =&gt; 'form-control'}) %&gt;
        &lt;/div&gt;              
        &lt;%= ff.label(:quantity, "Quantity:") %&gt;
        &lt;div class="input-group"&gt;
            &lt;div class="input-group-addon"&gt;
                &lt;i class="fa fa-shopping-cart fa-lg" aria-hidden="true"&gt;&lt;/i&gt;
            &lt;/div&gt;
            &lt;%= ff.select(:quantity, options_for_select(1..10),{}, {:class =&gt; 'form-control', :placeholder =&gt; 'Number'}) %&gt;
        &lt;/div&gt;          
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;% z = z+1 %&gt;
    &lt;% end %&gt;
</code></pre>

<p>What is wrong with the code or what am I missing to achieve the change in <code>:quantity</code> that I need when updating an invoice?</p>