2011-12-13 89 views
1

我正在開發使用Rails 3.1.2的應用程序,但我不能找到一些文件,在這個版本軌有錯誤/異常的作品(如404)。Ruby on Rails的3.1全局異常處理

我已經嘗試了諸如:

在應用控制器

rescue_from ActiveRecord::RecordNotFound,ActionController::RoutingError, 
      ActionController::UnknownController, ActionController::UnknownAction, :NoMethodError, :with => :handle_exception 

    def handle_exception 
    render :template => 'error_pages/error' 
    end 

environment/development.rb 

config.consider_all_requests_local = false 

我在哪裏可以找到一個解決辦法?

在此先感謝...

回答

4

這應該工作:

在應用控制器

class NotFound < StandardError; end 
    rescue_from NotFound, :with => :handle_exception 

    def handle_exception 
    render :template => 'error_pages/error' 
    end 
+0

我確認:這個作品。 – 2012-02-07 11:14:29

0

action_dispatch/middleware/show_exceptions

從源文件:

# This middleware rescues any exception returned by the application 
# and wraps them in a format for the end user. 

短的故事,總之,它呈現ActionDispatch::ShowExceptions.render_exception當包裹的應用程序(Rails的,你的情況),遇到unrescued例外。

如果你通過默認的實現,它最終呈現類似public/500.html,這就是您在生產環境中看到。按照您認爲適合添加自己的實現的方式覆蓋該方法或方法鏈。