处理rails中已删除的记录

时间:2014-02-21 10:22:01

标签: ruby-on-rails-3.1

我正在尝试阻止我的应用程序显示错误“无法找到id = 53的ReceiptVoucher”。实际上我在做什么我在第一个打开两个窗口我列出所有记录,在第二个窗口我正在访问特定记录假设记录ID为53.现在我已从索引页面删除该记录,并再次尝试从先前打开的第二个窗口。

这是我的控制器删除操作:

def destroy
    @receipt_voucher = ReceiptVoucher.find(params[:id])
  if @receipt_voucher.blank?
  redirect_to(receipt_vouchers_url, :notice => "record not found")  
  else
    @receipt_voucher.destroy
    respond_to do |format|
      @receipt_voucher.register_user_action(request.remote_ip, 'deleted')
      format.html { redirect_to(receipt_vouchers_url, :notice => "receipt voucher has been successfully deleted") }
    end
 end

如何在控制器中处理此问题。任何帮助都会感激不尽

1 个答案:

答案 0 :(得分:0)

请改为:@receipt_voucher = ReceiptVoucher.where(id: params[:id]).first