Fallback to a default controller action

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.

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

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…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs