# Fallback to a default controller action

**URL:** <https://www.ruby-forum.com/t/fallback-to-a-default-controller-action/115993>\
**Category:** Rails\
**Created:** [October 1, 2007, 2:16pm UTC](https://www.ruby-forum.com/t/fallback-to-a-default-controller-action/115993 "2007-10-01T14:16:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 1, 2007, 2:16pm UTC](https://www.ruby-forum.com/t/fallback-to-a-default-controller-action/115993/1 "2007-10-01T14:16:00Z")

</div>

What is the proper way to have any invalid request fall back to a  
specific action and controller?

That is, instead of seeing an error like those below, I would like  
control to pass to my main controller action.

The error "no route found to match “/foo with {:method=\>:get}” is shown  
if the controller is invalid.  
The error “No action responded to foo” is shown if the controller is  
valid but the action isn’t.

Many thanks.

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 1, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/fallback-to-a-default-controller-action/115993/2 "2007-10-01T16:33:19Z")

</div>

On 1 Oct 2007, at 13:16, John L. wrote:

> What is the proper way to have any invalid request fall back to a  
> specific action and controller?
> 
> That is, instead of seeing an error like those below, I would like  
> control to pass to my main controller action.

I think you need to implement rescue\_action. This gets called  
whenever an exception is thrown, so you need to check whether the  
exception corresponds to the missing action one and then do as  
appropriate.

Rails 2.0 has a much nicer way of dealing with this, since you can do

class PostsController \< ApplicationController  
rescue\_from FooError, :with =\> :foo\_handler

```
 protected
   def foo_handler
   end

```

end

Fred

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 1, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/fallback-to-a-default-controller-action/115993/3 "2007-10-01T17:23:57Z")

</div>

> I think you need to implement rescue\_action.

Thanks Fred. Yes, this works if I put this in my application.rb:

def rescue\_action(exception)  
redirect\_to :controller =\> :centre, :action =\> :index  
end

I guess I ought to also include the code from the original rescue\_action  
that logs the error…
