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.)