Ruby on rails 将所有控制器操作包装在开始营救中以记录错误

Ruby on rails 将所有控制器操作包装在开始营救中以记录错误,ruby-on-rails,error-handling,rollbar,Ruby On Rails,Error Handling,Rollbar,我最近安装了rails应用程序。它正在报告错误,但并不总是与上下文相关。为了获得上下文,您需要捕获异常并传入错误 begin # code... rescue => e Rollbar.error(e) 有没有一种rails方法可以通过上下文捕获异常 也许你用什么东西包装应用程序控制器?在Django中,您可以对视图进行子类化…假设您的所有控制器都继承自ApplicationController,您可以在ApplicationController中使用rescue\u来挽救任何控

我最近安装了rails应用程序。它正在报告错误,但并不总是与上下文相关。为了获得上下文,您需要捕获异常并传入错误

begin
  # code...
rescue => e
  Rollbar.error(e)
有没有一种rails方法可以通过上下文捕获异常


也许你用什么东西包装应用程序控制器?在Django中,您可以对视图进行子类化…

假设您的所有控制器都继承自ApplicationController,您可以在ApplicationController中使用
rescue\u来挽救任何控制器中的任何错误

ApplicationController < ActionController::Base

  rescue_from ActiveRecord::RecordNotFound do |exception|
    message = "Couldn't find a record."
    redirect_to no_record_url, info: message
  end

end
ApplicationController < ActionController::Base

  rescue_from do |exception|
    message = "some unspecified error"
    redirect_to rescue_message_url, info: message
  end

  rescue_from ActiveRecord::RecordNotFound do |exception|
    message = "Couldn't find a record."
    redirect_to rescue_message_url, info: message
  end

end