How do I use Ruby refinements inside Rails Controller/Action?

1.4k Views Asked by At

If I try outside the controller it works:

using ParamsExtension
class ApplicationController

If I try inside the controller or an action it does not work:

class ApplicationController
using ParamsExtension

It throws 'undefined method `using'.

I read this article and the author is using it inside a class: timelessrepo.com/refinements-in-ruby

2

There are 2 best solutions below

3
On

I read the following from the documentation:

You may only activate refinements at top-level, not inside any class, module or method scope. You may activate refinements in a string passed to Kernel#eval that is evaluated at top-level. Refinements are active until the end of the file or the end of the eval string, respectively.

http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html

0
On

Based on this article, refinements within classes are only available on 2.3 and up.