我几乎尝试了网上的所有内容,我想要的只是在出现"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)