Rescue_from ActionController::RoutingError

Relative newb here.

Is there a relatively simple way to gracefully trap routing errors?

I seem to be in the same boat with a lot of other people …

In application.rb I have

#----------------

Filters added to this controller apply to all controllers in the

application.

Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time

See ActionController::RequestForgeryProtection for details

Uncomment the :secret if you’re not using the cookie session store

protect_from_forgery # :secret => ‘34e000fc7cc2daeae150a89535f7f87d’

Be sure to include AuthenticationSystem in Application Controller

instead
include AuthenticatedSystem

#Shnelvar
debugger # This one gets hit
rescue_from ActionController::RoutingError, :with => :route_not_found

rescue_from ActionController::RoutingError, :with => :render_404

protected

def route_not_found
debugger #this one does not
render :text => ‘What are you looking for ?’, :status => :not_found
end
#Shnelvar end

end

#----------------

Tracing through the code, the first breakpoint gets hit. The second
does not.

Further tracing shows that the line
rescue_handlers << [key, options[:with]]
in
\ruby\lib\ruby\gems\1.8\gems\actionpack-2.0.2\lib\action_controller\rescue.rb
seems to be working.

I have looked in these archives. I have googeled things related to
rescue_from ActionController::RoutingError
and smoe people get this to work. Some people need to put stuff in
lower level controllers … but I have not gotten that to work, either.

Ralph S. wrote:

Relative newb here.

Is there a relatively simple way to gracefully trap routing errors?

I know this is not what you want to hear, but I consider returning the
404 page to be “graceful.” That’s what the HTTP 404 status code, and
page, are for anyway. I would just customize the 404 page and be done
with it.

Sorry I don’t have a more direct answer to your specific question.

Robert W. wrote:

Ralph S. wrote:

Relative newb here.

Is there a relatively simple way to gracefully trap routing errors?

I know this is not what you want to hear, but I consider returning the
404 page to be “graceful.” That’s what the HTTP 404 status code, and
page, are for anyway. I would just customize the 404 page and be done
with it.

Agreed. You should never get routing errors unless a user mistypes a
URL – and that’s what the 404 page is for. That’s all you need.

Sorry I don’t have a more direct answer to your specific question.

That is a direct answer.

BTW: why the “#Shnelvar”? Aren’t you using version control?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Ralph S. wrote:

Marnen Laibow-Koser wrote:

BTW: why the “#Shnelvar”? Aren’t you using version control?

My partner and I have not settle on a version control system to use yet.

I recommend Git, since distributed version control is the way to go;
Mercurial might be a second choice. But pick one and use it. There is
no excuse at all for working without version control even if you’re a
solo developer; with a partner, that’s even more true. (Same for
automated tests, BTW.)

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

BTW: why the “#Shnelvar”? Aren’t you using version control?

My partner and I have not settle on a version control system to use yet.

OK … so how do I get a 404 error instead of a

Routing Error

No route matches “/xxx” with {:method=>:get}

Ralph

Robert W. wrote:

Ralph S. wrote:

Relative newb here.

Is there a relatively simple way to gracefully trap routing errors?

I know this is not what you want to hear, but I consider returning the
404 page to be “graceful.” That’s what the HTTP 404 status code, and
page, are for anyway. I would just customize the 404 page and be done
with it.

Sorry I don’t have a more direct answer to your specific question.

Marnen Laibow-Koser wrote:

Ralph S. wrote:

Marnen Laibow-Koser wrote:

BTW: why the “#Shnelvar”? Aren’t you using version control?

My partner and I have not settle on a version control system to use yet.

I recommend Git, since distributed version control is the way to go;
Mercurial might be a second choice. But pick one and use it. There is
no excuse at all for working without version control even if you’re a
solo developer; with a partner, that’s even more true. (Same for
automated tests, BTW.)

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

My partner suggests SVN. Since we have not settled on anything yet …
and it seems to be either Git or SVN.

Ralph S. wrote:

My partner suggests SVN. Since we have not settled on anything yet …
and it seems to be either Git or SVN.

Use Git, then. SVN, as a centralized version control system, is far
more limited than Git. It’s a little easier to learn, but far less
capable. Most Rails developers – including me, as well as the core
team – left SVN for Git some time ago.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Ralph S. wrote:

OK … so how do I get a 404 error instead of a

Routing Error

No route matches “/xxx” with {:method=>:get}

Stop fighting Rails! AFAIK, Rails already returns HTTP status 404 if it
hits a RoutingError. You don’t need to do anything else except
customize 404.html.

Ralph

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Ralph S. wrote:

I am not fighting it.

I am getting a

Routing Error

No route matches “/xxx” with {:method=>:get}

rather than a 404 error during development.

So … why am I getting that message instead of a 404 here?

Because Rails handles errors differently in development and production.
In development, you see diagnostic info; in production, you see
404.html. (I suspect the HTTP status code is 404 even in development,
though; you could check with curl -i.)

And please don’t top-post.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I am not fighting it.

I am getting a

Routing Error

No route matches “/xxx” with {:method=>:get}

rather than a 404 error during development.

So … why am I getting that message instead of a 404 here?

Marnen Laibow-Koser wrote:

Ralph S. wrote:

OK … so how do I get a 404 error instead of a

Routing Error

No route matches “/xxx” with {:method=>:get}

Stop fighting Rails! AFAIK, Rails already returns HTTP status 404 if it
hits a RoutingError. You don’t need to do anything else except
customize 404.html.

Ralph

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This is posted years after the fact, but just in case anyone arrives via
a web search:

The rescue_from method shown above will NOT catch routing errors in
Rails 3.1 and Rails 3.2. In particular, this won’t work:

# file: app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
  rescue_from ActionController::RoutingError, :with => :not_found
  ...

You can read all about it (and suggested workarounds) here:
Can no longer rescue_from ActionController::RoutingError · Issue #671 · rails/rails · GitHub
and here (point #3):
My five favorite “hidden” features in Rails 3.2 « Plataformatec Blog

HTH.