Rails 3.1错误捕捉

问题描述

| 我认为Rails 3.1正在改变引发错误的方式。任何人都可以协助或确认吗?我正在尝试使用Rails 3.1.0.rc1创建自定义错误页面
unless config.consider_all_requests_local
    rescue_from Exception,:with => :render_error
    rescue_from ActiveRecord::RecordNotFound,:with => :render_not_found
    rescue_from ActionController::RoutingError,:with => :render_not_found
    rescue_from ActionController::UnknownController,:with => :render_not_found
    rescue_from ActionController::UnknownAction,:with => :render_not_found
end
^^这不起作用。
config.consider_all_requests_local       = true
默认情况下在我的开发环境中。我假设Rails 3.1删除了“ action_controller”,但是我无法在任何地方确认这一点。 谢谢!     

解决方法

我假设以下代码出现在您的ApplicationController中?
unless config.consider_all_requests_local
  rescue_from Exception,:with => :render_error
  rescue_from ActiveRecord::RecordNotFound,:with => :render_not_found
  rescue_from ActionController::RoutingError,:with => :render_not_found
  rescue_from ActionController::UnknownController,:with => :render_not_found
  rescue_from ActionController::UnknownAction,:with => :render_not_found
end
如果是这样,请尝试替换此行:
unless config.consider_all_requests_local
这行(我认为是Rails 3之前的版本):
unless ActionController::Base.consider_all_requests_local
或此(Rails 3之后):
unless Rails.application.config.consider_all_requests_local
    ,我不相信Matt的解决方案会在Rails 3.0 / 3.1中捕获路由错误。 尝试将以下内容放入application.rb中:
# 404 catch all route
config.after_initialize do |app|
  app.routes.append{ match \'*a\',:to => \'application#render_not_found\' } unless config.consider_all_requests_local
end
参见:https://github.com/rails/rails/issues/671#issuecomment-1780159 对我来说很好!     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...