Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yuki24
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuki24/09f373a318a4ac56f5a9 to your computer and use it in GitHub Desktop.
Save yuki24/09f373a318a4ac56f5a9 to your computer and use it in GitHub Desktop.
Error handler that generates error pages dynamically in Rails
# config/application.rb
config.exceptions_app = ->(env) { ErrorsController.action(:show).call(env) }
# app/controllers/errors_controller.rb
class ErrorsController < ActionController::Base
layout 'application'
rescue_from StandardError, with: :internal_server_error
rescue_from ActiveRecord::RecordNotFound, ActionController::RoutingError, with: :not_found
def not_found(exception = nil)
render template: "errors/not_found", status: 404
end
def internal_server_error(exception = nil)
render template: "errors/internal_server_error", status: 500
end
def show
raise env["action_dispatch.exception"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment