使用`config.exceptions_app = self.routes`在Rails 3.2中解决错误

根据这篇文章:

My five favorite “hidden” features in Rails 3.2

处理错误的最新方法如​​下所示:

# application.rb: config.exceptions_app = self.routes #routes.rb match "/404", to: "site#not_found" 

但是,他没有说明rails错误应用程序还处理500个错误,422个错误(以及可能还有其他错误传递给这两个页面的事实?)

所以我一起攻击了一个看起来像这样的解决方案:

 # routes.rb rack_error_handler = ActionDispatch::PublicExceptions.new('public/') match "/422" => rack_error_handler match "/500" => rack_error_handler 

它的好处在于它使我的500页轻量级。

我还应该抓住其他错误吗? 我的理解是,虽然500页现在将使用两个机架应用程序,但它仍然可以安全地与主Rails应用程序隔离。 这很强吗?

谢谢!

我在应用程序控制器中添加了救援

  if Rails.env.production? 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 rescue_from ActionView::MissingTemplate, :with => :render_not_found end def render_not_found(exception) logger.info("render_not_found: #{exception.inspect}") redirect_to root_path, :notice => 'The page was not found.' end 

然后添加一个errors_controller以解决路由错误,将其添加到我的路由文件的底部

  match "*path", :to => "errors#routing_error" 

试试这个

更新config/application.rb

 config.exceptions_app = self.routes 

和你的路线文件

 match "/404", :to => "errors#not_found"