Created
October 8, 2014 04:18
-
-
Save satomixx/1460b73aae14bdd35021 to your computer and use it in GitHub Desktop.
[Rails 4.x] エラーページの作り方 ref: http://qiita.com/tsumekoara/items/88815b94f63608a801a8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ErrorHandlers | |
extend ActiveSupport::Concern | |
included do | |
rescue_from Exception, with: :rescue500 | |
rescue_from ApplicationController::Forbidden, with: :rescue403 | |
rescue_from ApplicationController::IpAddressRejected, with: :rescue403 | |
rescue_from ActionController::RoutingError, with: :rescue404 | |
rescue_from ActiveRecord::RecordNotFound, with: :rescue404 | |
end | |
private | |
def rescue403(e) | |
@exception = e | |
render 'errors/forbidden', status: 403 | |
end | |
def rescue404(e) | |
@exception = e | |
render 'errors/not_found', status: 404 | |
end | |
def rescue500(e) | |
@exception = e | |
render '/errors/internal_server_error', status: 500 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Forbidden < ActionController::ActionControllerError; end | |
class IpAddressRejected < ActionController::ActionControllerError; end | |
include ErrorHandlers if Rails.env.production? or Rails.env.staging? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '*anything' => 'errors#routing_error' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ErrorsController < ApplicationController | |
def routing_error | |
raise ActionController::RoutingError, "No route matches #{request.path.inspect}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment