views:

51

answers:

2

Hello,

I defined a before filter in my controller:

before_filter :find,  :only => [:caller]

and I want to catch exceptions in "find" method :

def find 
   begin
     ...
   rescue Exception
     redirect_to somewhere
   end
 end

but how can I prevent the "caller" method from continuing executing ?

A: 

Have you tried return after the redirect in the rescue?

    redirect_to ...
    return # <<
  end
end
nowk
Patrick Reagan
+1  A: 

If a before_filter renders or redirects, the execution stops automatically.

Learn more: http://guides.rubyonrails.org/action_controller_overview.html#filters

For ActiveRecord callbacks like before_validation, use return false to stop the record from being saved.

Vikrant Chaudhary
Thank you very much !It's very helpful !