Controller methods unavailable inside RJS block

I just came to a sad realisation: any method you declare inside a controller (such as a private helper method) is not available inside a RJS block. I can see why (blocks introduce scope) but it’s still annoying.

Here’s the thing: to avoid duplication I moved a certain test to it’s own private method. Works fine inside any controller action except inside a RJS block. Sure I understand why. But ‘regular’ global variables such as session and params are available.

It would only seem logical to export any private methods to the RJS scope as well. Sadly, that is not the case. So my pityful solution is to store the method in a local variable. Does the trick alright.

# inside update :render RJS block
matching_captcha = captcha_matches? # needed to pull method inside scope of rjs block!

# at the bottom of the controller
private

def captcha_matches?
  params[:captcha] == session[:captcha]
end