ruby-on-rails – 尝试访问破碎的图片网址时,内部服务器错误500被抛出而不是404

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 尝试访问破碎的图片网址时,内部服务器错误500被抛出而不是404前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们有一个rails服务器,使用本教程在此设置自定义404和500页:

http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

虽然它工作很好,并为各种路径抛出404,它会生成内部服务器错误500,同时尝试访问任何类型的后缀路径,如en / foo.png,en / foo.pdf,en / foo.xml,…

但是像en / file.foo这样的东西会抛出404.所以只有有效的后缀会抛出500.

路线结束:

  1. if Rails.application.config.consider_all_requests_local
  2. match '*not_found',to: 'errors#error_404'
  3. end

application_controller.rb

  1. unless Rails.application.config.consider_all_requests_local
  2. rescue_from Exception,with: :render_500
  3. rescue_from ActionController::RoutingError,with: :render_404
  4. rescue_from ActionController::UnknownController,with: :render_404
  5. rescue_from ::AbstractController::ActionNotFound,with: :render_404
  6. rescue_from ActiveRecord::RecordNotFound,with: :render_404
  7. end
  8.  
  9. protected
  10.  
  11. def render_404(exception)
  12. @not_found_path = exception.message
  13. respond_to do |format|
  14. format.html { render template: 'errors/error_404',layout: 'layouts/application',status: 404 }
  15. format.all { render nothing: true,status: 404 }
  16. end
  17. end
  18.  
  19. def render_500(exception)
  20. logger.fatal(exception)
  21. respond_to do |format|
  22. format.html { render template: 'errors/error_500',status: 500 }
  23. format.all { render nothing: true,status: 500}
  24. end
  25. end

500出现:

  1. Missing template errors/error_404 with {:locale=>[:de,:en],:formats=>[:png],:handlers=>[:erb,:builder,:coffee,:arb,:haml]}

解决方法

我们发现错误.

我们有一个error_controller.rb包含这个:

  1. def error_404
  2. @not_found_path = params[:not_found]
  3. render template: 'errors/error_404',status: 404
  4. end

我们改变了这个问题来解决这个问题:

  1. def error_404
  2. @not_found_path = params[:not_found]
  3. respond_to do |format|
  4. format.html { render template: 'errors/error_404',status: 404 }
  5. end
  6. end

猜你在找的Ruby相关文章