Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to catch :abort signal thrown in Rails 5 controller

In Rails 5, when a callback needs to cancel later callbacks, the recommended process is documented as "you should explicitly throw :abort."

My question is: how is it recommended that this exception be caught?

My current solution is to catch UncaughtThrowError in my ApplicationController - given the way it's documented, I thought this feature would trigger some magic in Rails or a Rack middleware to immediately move to the rendering (ActionView) phase.

like image 702
sameers Avatar asked Jan 29 '17 17:01

sameers


1 Answers

Here are some examples of how to use the throw/catch in ruby.

For Rails, I think something like this will do the trick:

class SomeController 
  def some_method 
    if catch(:abort) { mode.save }
      # success
    else
      # failure 
    end
  end
end
like image 90
ekampp Avatar answered Sep 24 '22 03:09

ekampp