ruby-on-rails – 使用`config.exceptions_app = self.routes`在
发布时间:2020-12-17 04:39:56 所属栏目:百科 来源:网络整理
导读:根据这篇文章: http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/ 处理错误的最新方法如??下所示: # application.rb:config.exceptions_app = self.routes#routes.rbmatch "/404",to: "site#not_found" 但是,他没
根据这篇文章:
http://blog.plataformatec.com.br/2012/01/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页轻量级. 我还应该抓住其他错误吗? 谢谢! 解决方法
我在应用程序控制器中添加了救援
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" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |