Bill Garrison
Bill Garrison

Reputation: 2227

sending _destroy param not deleting association

I have an invoice which has many invoice lines. On Edit I use javascript to set a hidden _destroy field to a value of 1 when the delete button is clicked for an invoice line. When submitted it should delete the items. It was working but it randomly stopped today.

Here are the parameters sent in:

Parameters: {
"recycler_invoice"=>{
  "number"=>"IT10059",
  "customer_id"=>"1",
  "date_entered"=>"10/04/2013",
  "gold_spot_price_paid"=>"1248.12",
  "silver_spot_price_paid"=>"22.46",
  "platinum_spot_price_paid"=>"1206.13",
  "refine_fee_percentage"=>"5.0", 
  "consulting_fee_percentage"=>"3.0",
  "verify_fee_percentage"=>"2.0",               
  "recycler_invoice_lines_attributes"=>{
   "0"=>{"denomination_id"=>"11", "quantity"=>"315.767", "_destroy"=>"false", "id"=>"1023"},
   "1"=>{"denomination_id"=>"7", "quantity"=>"78.466", "_destroy"=>"false", "id"=>"1024"}, 
   "2"=>{"denomination_id"=>"1", "quantity"=>"1.174", "_destroy"=>"false", "id"=>"1025"}, 
   "3"=>{"denomination_id"=>"2", "quantity"=>"7.46", "_destroy"=>"false", "id"=>"1026"}, 
   "4"=>{"denomination_id"=>"9", "quantity"=>"28.909", "_destroy"=>"false", "id"=>"1027"}, 
   "5"=>{"denomination_id"=>"10", "quantity"=>"9.629", "_destroy"=>"false", "id"=>"1028"}, 
   "6"=>{"denomination_id"=>"13", "quantity"=>"19.77", "_destroy"=>"false", "id"=>"1029"}, 
   "7"=>{"denomination_id"=>"14", "quantity"=>"5.83", "_destroy"=>"false", "id"=>"1030"}, 
   "8"=>{"denomination_id"=>"22", "quantity"=>"240.37", "_destroy"=>"1", "id"=>"1031"}}},
"commit"=>"Submit",
"id"=>"9"}

As you can see the one line that I want to delete has _destroy of 1.

Here is my update code...pretty straightforward:

def update
    @recycler_invoice = RecyclerInvoice.find(params[:id])

    respond_to do |format|
      if @recycler_invoice.update_attributes(params[:recycler_invoice])
        format.html { redirect_to @recycler_invoice, notice: 'Recycler invoice was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @recycler_invoice.errors, status: :unprocessable_entity }
      end
    end
  end

Upvotes: 0

Views: 73

Answers (1)

Ivan Etchart
Ivan Etchart

Reputation: 36

I guess in your RecyclerInvoice you have added accepts_nested_attributes_for :recycler_invoice_lines, if so, then you probably forgot to add the option allow_destroy: true to it.

Upvotes: 1

Related Questions