-
-
Save Archimidis/6104523 to your computer and use it in GitHub Desktop.
Custom dynamic 404 and 500 pages
This file contains hidden or 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 ApplicationController < ActionController::Base | |
| # ... | |
| unless Rails.application.config.consider_all_requests_local | |
| rescue_from Exception, with: :render_500 | |
| rescue_from ActionController::RoutingError, with: :render_404 | |
| rescue_from ActionController::UnknownController, with: :render_404 | |
| rescue_from ActionController::UnknownAction, with: :render_404 | |
| rescue_from ActiveRecord::RecordNotFound, with: :render_404 | |
| end | |
| private | |
| def render_404(exception) | |
| @not_found_path = exception.message | |
| respond_to do |format| | |
| format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 } | |
| format.all { render nothing: true, status: 404 } | |
| end | |
| end | |
| def render_500(exception) | |
| @error = exception | |
| respond_to do |format| | |
| format.html { render template: 'errors/error_500', layout: 'layouts/application', status: 500 } | |
| format.all { render nothing: true, status: 500} | |
| end | |
| end | |
| # ... | |
| end |
This file contains hidden or 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
| %h2 404 | |
| %div | |
| %h3 We're sorry | |
| %p | |
| The content that you requested could not be found. | |
| %p | |
| You tried to access '#{@not_found_path}', which is not a valid page. | |
| %p | |
| Want to | |
| %a{href: root_path} go back to our home page | |
| and try again? |
This file contains hidden or 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 error_404 | |
| @not_found_path = params[:not_found] | |
| end | |
| def error_500 | |
| end | |
| end |
This file contains hidden or 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
| rails generate controller errors error_404 error_505 |
This file contains hidden or 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 "errors/error_404" | |
| get "errors/error_500" |
This file contains hidden or 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
| unless Rails.application.config.consider_all_requests_local | |
| match '*not_found', to: 'errors#error_404' | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment