Custom Rails error pages
In ApplicationController
:
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: lambda { |exception| render_error 500, exception }
rescue_from ActionController::RoutingError, ActionController::UnknownController, ::AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception }
end
private
def render_error(status, exception)
render template: "errors/error_#{status}", layout: 'layouts/application', status: status
end
Written by Emmanuel Turlay
Related protips
2 Responses
Consider using exceptions_app to also handle exceptions in the middleware layer with a custom error page https://coderwall.com/p/w3ghqq
over 1 year ago
·
@jeroenr Thank you for that tip, I was looking for that exactly ;)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#