发送_destroy param不删除关联

时间:2013-04-10 22:03:22

标签: ruby ruby-on-rails-3 activerecord destroy

我的发票上有很多发票行。在编辑时,当单击发票行的删除按钮时,我使用javascript将隐藏的_destroy字段设置为值1。提交时,应删除这些项目。它工作正常,但它今天随机停止了。

以下是发送的参数:

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"}

正如您所看到的那样,我要删除的一行有_destroy为1.

这是我的更新代码......非常简单:

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

1 个答案:

答案 0 :(得分:1)

我想在您的RecyclerInvoice中添加了accepts_nested_attributes_for:recycler_invoice_lines,如果是这样,那么您可能忘记将选项allow_destroy:true添加到其中。