Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Archimidis/6104523 to your computer and use it in GitHub Desktop.
Save Archimidis/6104523 to your computer and use it in GitHub Desktop.

Revisions

  1. @gonzedge gonzedge revised this gist Sep 24, 2012. 1 changed file with 5 additions and 17 deletions.
    22 changes: 5 additions & 17 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -2,27 +2,15 @@ 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
    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_404(exception)
    @not_found_path = exception.message
    def render_error(status, exception)
    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}
    format.html { render template: "errors/error_#{status}", layout: 'layouts/application', status: status }
    format.all { render nothing: true, status: status }
    end
    end

  2. @gonzedge gonzedge revised this gist Apr 1, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generate.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    rails generate controller errors error_404 error_505
    rails generate controller errors error_404 error_500
  3. @gonzedge gonzedge revised this gist Feb 6, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -13,15 +13,15 @@ class ApplicationController < ActionController::Base
    def render_404(exception)
    @not_found_path = exception.message
    respond_to do |format|
    format.html { render template: 'errors/error_404', status: 404 }
    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', status: 500 }
    format.html { render template: 'errors/error_500', layout: 'layouts/application', status: 500 }
    format.all { render nothing: true, status: 500}
    end
    end
  4. @gonzedge gonzedge revised this gist Jan 16, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -10,15 +10,15 @@ class ApplicationController < ActionController::Base
    end

    private
    def render_404(error)
    def render_404(exception)
    @not_found_path = exception.message
    respond_to do |format|
    format.html { render template: 'errors/error_404', status: 404 }
    format.all { render nothing: true, status: 404 }
    end
    end

    def render_500(error)
    def render_500(exception)
    @error = exception
    respond_to do |format|
    format.html { render template: 'errors/error_500', status: 500 }
  5. @gonzedge gonzedge created this gist Jan 5, 2012.
    30 changes: 30 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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(error)
    @not_found_path = exception.message
    respond_to do |format|
    format.html { render template: 'errors/error_404', status: 404 }
    format.all { render nothing: true, status: 404 }
    end
    end

    def render_500(error)
    @error = exception
    respond_to do |format|
    format.html { render template: 'errors/error_500', status: 500 }
    format.all { render nothing: true, status: 500}
    end
    end

    # ...
    end
    11 changes: 11 additions & 0 deletions error_404.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    %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?
    8 changes: 8 additions & 0 deletions errors_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class ErrorsController < ApplicationController
    def error_404
    @not_found_path = params[:not_found]
    end

    def error_500
    end
    end
    1 change: 1 addition & 0 deletions generate.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    rails generate controller errors error_404 error_505
    2 changes: 2 additions & 0 deletions remove_routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    get "errors/error_404"
    get "errors/error_500"
    3 changes: 3 additions & 0 deletions routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    unless Rails.application.config.consider_all_requests_local
    match '*not_found', to: 'errors#error_404'
    end