加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

我还应该抓住其他错误吗?
我的理解是,虽然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"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读