Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:45:55+00:00 2026-05-29T11:45:55+00:00

Is there are way to tell Rails to render your custom error pages (for

  • 0

Is there are way to tell Rails to render your custom error pages (for example, the ones you write in your ErrorsController)? I’ve searched many topics, and the one that seems to kinda work was to add to your ApplicationController something like

if Rails.env.production?
  rescue_from Exception, :with => :render_error
  rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
  rescue_from ActionController::UnknownController, :with => :render_not_found
  rescue_from ActionController::UnknownAction, :with => :render_not_found
end

and then you write your methods render_error and render_not_found the way you want. This seems to me like a really unelegant solution. Also, it’s bad, because you have to know exactly what are all the errors that could happen. It’s a temporary solution.

Also, there is really no easy way to rescue an ActionController::RoutingError that way. I saw that one way was to add something like

get "*not_found", :to => "errors#not_found"

to your routes.rb. But what if you want to raise somewhere an ActionController::RoutingError manually? For example, if a person who is not an administrator tries to go to “adminy” controllers by guessing the URL. In those situations I prefer raising a 404 more than raising an “unauthorized access” error of some kind, because that would actually tell the person that he guessed the URL. If you raise it manually, it will try to render a 500 page, and I want a 404.

So is there a way to tell Rails: “In all cases you would normally render a 404.html or a 500.html, render my custom 404 and 500 pages”? (Of course, I deleted the 404.html and 500.html pages from the public folder.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:45:55+00:00Added an answer on May 29, 2026 at 11:45 am

    Unfortunately there aren’t any methods I know of that can be overridden to provide what you want. You could use an around filter. Your code would look something like this:

    class ApplicationController < ActionController::Base
      around_filter :catch_exceptions
    
      protected
        def catch_exceptions
          yield
        rescue => exception
          if exception.is_a?(ActiveRecord::RecordNotFound)
            render_page_not_found
          else
            render_error
          end
        end
    end
    

    You can handle each error as you see fit in that method. Then your #render_page_not_found and #render_error methods would have to be something like

    render :template => 'errors/404'
    

    You would then need to have a file at app/views/errors/404.html.[haml|erb]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to tell Visual Studio 2005 to not rearrange your SSIS
Is there any way (middleware, etc) to tell the rails router that any route
Is there any way to tell Yard not to clutter up my Rails project's
Is there any way to tell GCC to not initialize a particular global array
Is there any way to tell db4o to call class default constructor while activating
Is there a way to tell excel to not display non-significant series in a
Is there a way to tell ifort or gfortran to just try to parse
Is there a way to tell the php complier that I want a specific
is there some way to tell sql server to use the (nolock) hint or
Is there a way to tell scons to use a particular file to setup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.