2011-05-25 57 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