【发布时间】:2014-09-01 22:40:54
【问题描述】:
我的 ApplicationController 类中有自定义错误处理代码:
unless ActionController::Base.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
end
但是,如果我输入一个混搭且无效的 URL,例如:'/we/efe/qwe/wqeqw/qwe' 带有config.consider_all_requests_local = false 的系统会给我标准的 Rails 404 页面而不是被捕获:
rescue_from ActionController::RoutingError, :with => :render_not_found
查看异常跟踪日志,显示 Rails 正在引发 ActionController::RoutingError (No route matches [GET] "/we/efe/qwe/wqeqw/qwe"):,但我无法确定为什么它没有被我的处理程序捕获。
为什么这不起作用?
【问题讨论】:
-
能否请您发布
render_not_found方法。并从公共文件夹中删除原来的 404.html 和 500.html。 -
这是开发还是测试还是生产环境?
-
不需要 - 显然它是 Rails 3.2+ 引发 ActionController::RoutingError 的方式 - 答案是您需要在路由底部添加一个默认命名路由(捕获所有请求未路由:
get '*unmatched_route', :to => 'application#no_route'.)
标签: ruby-on-rails exception-handling