小编Dgl*_*mut的帖子

我每次在Rails 3上获得异常时都可以执行一个方法吗?

我几乎尝试了网上的所有内容,我想要的只是在出现"ActiveRecord :: RecordNotFound"或"No route matches"这样的异常时调用方法.

ApplicationController的救援工作不起作用,但为什么呢?

class ApplicationController < ActionController::Base
  protect_from_forgery

  private
    def self.send_report_error(message)
      Notifier.page_failure(message).deliver
    end

rescue ActiveRecord::RecordNotFound
  # handle not found error
  send_report_error ActiveRecord::RecordNotFound.to_s
rescue ActiveRecord::ActiveRecordError
  # handle other ActiveRecord errors
  send_report_error ActiveRecord::ActiveRecordError.to_s
rescue # StandardError
  # handle most other errors
  send_report_error "common error"
rescue Exception
  # handle everything else
  send_report_error "common exception"
end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-3

3
推荐指数
1
解决办法
105
查看次数

标签 统计

ruby ×1

ruby-on-rails ×1

ruby-on-rails-3 ×1