[英]ruby on rails 3.1 global exception handler
我正在使用Rails 3.1.2開發應用程序,但是在此版本的Rails上找不到能解決錯誤/異常(例如404)的文檔。
我嘗試過類似的事情:
在應用程序控制器中
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
在哪里可以找到解決方案?
提前致謝...
這應該工作:
在應用程序控制器中
class NotFound < StandardError; end
rescue_from NotFound, :with => :handle_exception
def handle_exception
render :template => 'error_pages/error'
end
查看action_dispatch / middleware / show_exceptions 。
從源代碼中的文檔中:
# This middleware rescues any exception returned by the application
# and wraps them in a format for the end user.
簡而言之,當包裝的應用程序(在您的情況下為Rails)遇到無法挽救的異常時,它將呈現ActionDispatch::ShowExceptions.render_exception
。
如果您查看默認的實現,最終public/500.html
類似於public/500.html
,這是您在生產環境中看到的。 覆蓋您認為合適的方法或方法鏈,以添加自己的實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.