ruby-on-rails – 使用`config.exceptions_app = self.routes`在Rails 3.2中解决错误

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 使用`config.exceptions_app = self.routes`在Rails 3.2中解决错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_1@根据这篇文章

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页轻量级.

我还应该抓住其他错误吗?
我的理解是,虽然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"
原文链接:https://www.f2er.com/ruby/269722.html

猜你在找的Ruby相关文章