【问题标题】:test 404 and 500 pages on rails develpment在 Rails 开发中测试 404 和 500 页
【发布时间】:2014-04-22 12:49:26
【问题描述】:

我正在尝试为我的 Rails 3.2.16 应用程序编写 404 和 500 页。我在application_controller.rb 中添加了以下内容:

  def local_request?
    false
  end

  binding.pry
  unless  ActionController::Base.consider_all_requests_local
    rescue_from Exception, with: :render_500
    rescue_from ActionController::RoutingError, with: :render_404
    rescue_from ActionController::UnknownController, with: :render_404
    rescue_from ActionController::UnknownAction, with: :render_404
    rescue_from ActiveRecord::RecordNotFound, with: :render_404
  end

  def render_404(exception)
    logger.warning exception.backtrace.join("\n")
    @not_found_path = exception.message
    respond_to do |format|
      format.html { render template: 'errors/not_found', layout: 'layouts/application', status: 404 }
      format.all { render nothing: true, status: 404 }
    end
  end

  def render_500(exception)
    logger.error exception.backtrace.join("\n")
    respond_to do |format|
      format.html { render template: 'errors/internal_server_error', layout: 'layouts/application', status: 500 }
      format.all { render nothing: true, status: 500}
    end
  end

虽然当我将浏览器设置为http://localhost:3000/nonexistent 时,我在日志中看到:

Started GET "/nonexistent" for 127.0.0.1 at 2014-04-22 13:45:12 +0100

ActionController::RoutingError (No route matches [GET] "/nonexistent"):
  actionpack (3.2.16) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.16) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.16) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.16) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.16) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.16) lib/rails/rack/logger.rb:16:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (3.2.16) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.16) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.16) lib/action_dispatch/middleware/static.rb:63:in `call'
  railties (3.2.16) lib/rails/engine.rb:484:in `call'
  railties (3.2.16) lib/rails/application.rb:231:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.16) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /Users/me/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /Users/me/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /Users/me/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /Users/me/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-3.2.16/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (3.2ms)

似乎 application_controller 甚至没有执行,因此它不会加载我的自定义 404 页面。

我不确定如何解决这个问题,以及如何在我的开发环境中可视化 404 和 500 页面,以确保它们看起来符合我的要求。

谢谢,

【问题讨论】:

  • 我有一个更好的方法给你——让我写一个答案

标签: ruby-on-rails ruby exception-handling http-status-code-404


【解决方案1】:

开发中.rb

改变

config.consider_all_requests_local = true

config.consider_all_requests_local = false

然后重新启动您的服务器。您的错误页面将在不进行所有调试的情况下显示。

【讨论】:

  • 谢谢,但我的 application_controller 仍然没有执行,(即 binding.pry 没有运行)。
  • 那是因为rails在找不到路由的时候不需要经过ActionController :)
  • 您可以通过调用 render_404 或 render_500 的操作来测试您的自定义 404 和 500 页面。当 rails 找不到路由时,它会在 public/404.html 中查找,但您可以更改此行为(查找 rails 异常应用程序)
【解决方案2】:

这是一个更好的方法:

#config/environments/production.rb
-> config/application.rb if you want on all environments
config.exceptions_app = ->(env) { ExceptionController.action(:show).call(env) }


#app/controllers/exception_controller.rb
class ExceptionController < ApplicationController
  respond_to :html, :xml, :json

    #Dependencies
    before_action :status

  #Layout
  layout :layout_status

  ####################
  #      Action      #
  ####################

    #Show
  def show
    respond_with status: @status
  end

  ####################
  #   Dependencies   #
  ####################

  protected

  #Info
  def status
    @exception  = env['action_dispatch.exception']
    @status     = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
    @response   = ActionDispatch::ExceptionWrapper.rescue_responses[@exception.class.name]
  end

  #Format
  def details
    @details ||= {}.tap do |h|
      I18n.with_options scope: [:exception, :show, @response], exception_name: @exception.class.name, exception_message: @exception.message do |i18n|
        h[:name]    = i18n.t "#{@exception.class.name.underscore}.title", default: i18n.t(:title, default: @exception.class.name)
        h[:message] = i18n.t "#{@exception.class.name.underscore}.description", default: i18n.t(:description, default: @exception.message)
      end
    end
  end
  helper_method :details

  ####################
  #      Layout      #
  ####################

  private

  #Layout
  def layout_status
    @status.to_s == "404" ? "application" : "error"
  end

end

#app/views/exception/show.html.haml
.box
%h1
    = details[:name]
%p
    = details[:message]

这行得通。如果您想在 dev 中运行,请使用 @interpidd 的答案

【讨论】:

  • 感谢您的好评,我已经尝试过了,添加:config.exceptions_app = -&gt;(env) { ExceptionController.action(:show).call(env) }/config/application.rb 并创建了包含所有内容的类。仍然当我转到 development env 中不存在的页面时,我看到默认路由错误页面:No route matches [GET] "/nonexistent"
  • 你进入config/environments/development.rb并设置config.consider_all_requests_local = false了吗?
  • 啊...好点,我认为您的解决方案没有必要这样做,但它仍然不起作用,现在当我进入不存在的页面时,我得到一个 500 错误页面。
  • 嗯好吧!你介意去聊聊这个吗?
  • 好的,我们认为脚本是为 Rails 4 编写的,而我在 Raisl 3.2.16 上,before_action 仅在新版本中引入,在旧版本中我需要改成before_filter,就可以了
【解决方案3】:

最好在生产模式下启动服务器并验证它。

rails s -eproduction

然后它将呈现错误页面。

【讨论】:

  • 好主意,不过有了这个我必须有一个production.rb 环境文件,它实际上是一个develompent 配置文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2013-08-03
  • 2011-02-13
  • 2011-11-27
  • 2012-10-23
  • 1970-01-01
  • 2013-03-31
相关资源
最近更新 更多