2011-05-25 59 views
10

我认为Rails 3.1正在改变错误发生的方式。任何人都可以协助或证实此事?我试图用Rails 3.1.0创建自定义错误页面.rc1Rails 3.1错误捕获

unless config.consider_all_requests_local 
    rescue_from Exception, :with => :render_error 
    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 
end 

^^这不起作用。

config.consider_all_requests_local  = true 

这是在我的开发环境默认情况下。我假设Rails 3.1删除了“action_controller”,但我无法在任何地方证实这一点。

谢谢!

+0

什么样的错误?一般来说是例外吗? – Matchu 2011-05-25 04:00:11

+0

如果您可以发布一些代码并解释您所看到的具体*意外行为,将会有所帮助。 – molf 2011-05-25 09:46:35

+0

你应该接受下面的答案。有用! – 2011-09-29 07:59:52

回答

19

我假设以下代码出现在您的ApplicationController中?

unless config.consider_all_requests_local 
    rescue_from Exception, :with => :render_error 
    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 
end 

如果是这样,请尝试更换这行:这一行

unless config.consider_all_requests_local 

(预Rails 3中,我认为):

unless ActionController::Base.consider_all_requests_local 

或本(后的Rails 3):

unless Rails.application.config.consider_all_requests_local 
9

我不相信马特的解决方案将捕获路由e Rails 3.0/3.1中的错误。

尝试把你的application.rb中如下:

# 404 catch all route 
config.after_initialize do |app| 
    app.routes.append{ match '*a', :to => 'application#render_not_found' } unless config.consider_all_requests_local 
end 

参见:https://github.com/rails/rails/issues/671#issuecomment-1780159

运作良好,对我来说!

+0

感谢您的加入!在这里发表我的原始答案几个星期后了解它。 :) – 2011-11-05 00:20:19

+0

一般情况下,最终是相等的。如果!Rails.env.production是危险的... – Kevin 2011-11-08 00:38:37

+0

@Kevin - 这是怎么发生的?这是否参考使用'consider_all_request_local'?如果是这样,我认为你没有提出有效的论点,因为这是一个基于每个环境定义的设置。每个环境都配置了许多设置,这只是众多设置之一。 – 2012-11-29 17:01:30