Why I cannot rescue_from with my custom method?

64 views
Skip to first unread message

Panayotis Matsinopoulos

unread,
Jul 2, 2014, 2:57:02 AM7/2/14
to plataforma...@googlegroups.com
Hi,

I am using devise and in my routes I have

authenticated :user do
....
end

So, some of my pages require authentication before being available to the user.

When being on development environment and I am trying to access a page that needs authenticated user, the application throws an exception:

ActionController::RoutingError (No route matches [GET] "/courses/8/preview"):
.....

I suppose that this one is thrown by devise gem code. Correct?

Then I am trying to rescue_from this error with my own custom method on ApplicationController. Like this:

rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, :with => :custom_404

But my method is never called and I still get the ActionController::RoutingError.

What am I missing here?


Thanks in advance
Panayotis


Long Tran

unread,
Jul 2, 2014, 3:15:55 AM7/2/14
to plataforma...@googlegroups.com
It seems rescue_from isn't working anymore in Rails 3.0.1+

Here's a proposed solution:

https://gist.github.com/Sujimichi/2349565

Long Tran

unread,
Jul 2, 2014, 3:19:30 AM7/2/14
to plataforma...@googlegroups.com
So mainly, we'll need to add a catch-all route:

#make this your last route.
match '*unmatched_route', :to => 'application#raise_not_found!'

And raise RoutingError:

#called by last route matching unmatched routes. Raises RoutingError which will be rescued from in the same way as other exceptions.
def raise_not_found!
raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")
end

Panayotis Matsinopoulos

unread,
Jul 2, 2014, 3:28:02 AM7/2/14
to plataforma...@googlegroups.com
Thanks this approach seems to be working.
Reply all
Reply to author
Forward
0 new messages