如何解决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
对我来说很好!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。