问题
I'm developing an app with Rails 3.1.2 but I can't find some documentation that works with errors / exception (like 404) on this version of rails.
i have tried things like:
In application controller
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
Where can I find a solution?
Thanks in advance...
回答1:
This should work:
In application controller
class NotFound < StandardError; end
rescue_from NotFound, :with => :handle_exception
def handle_exception
render :template => 'error_pages/error'
end
回答2:
Look at action_dispatch/middleware/show_exceptions.
From the documentation in the source:
# This middleware rescues any exception returned by the application
# and wraps them in a format for the end user.
Short story short, it renders ActionDispatch::ShowExceptions.render_exception
when the wrapped application (Rails, in your case), encounters an unrescued exception.
If you look through the default implementation, it ends up rendering something like public/500.html
, which is what you see in the production environment. Overwrite the method or method chain it as you see fit to add your own implementation.
来源:https://stackoverflow.com/questions/8495151/ruby-on-rails-3-1-global-exception-handler