How to use Ruby enhancements in Rails views?

I have a Rails 4 project using Ruby 2.0. I have identified some refinements. Enter

<% using MyRefinements %> 

at the top of the view files causes an "undefined method" error using "".

When I add:

 using MyRefinements 

At the top of my controller (above the class declaration), I can successfully use the refinement in the controller, but I get an "undefined" error if I try to use it in the view.

Thanks!

+8
ruby-on-rails-4 refinements
source share
2 answers

Unfortunately, this is not possible. For posterity, I document what I tried, which didn't work:

In view:

 <% using MyRefinements %> 

In the controller (each time separately):

 using MyRefinements helper :MyRefinements helper_method :MyRefinements helper { using MyRefinements } 

In the assistant:

 using MyRefinements 

Note that refinements become available in the controller and in the helper, but never in the view.

Too bad.

+1
source share

Using "use", you can import class refinements from a module into the current class or module definition. You cannot include it in a view file using 'using'. if you want to use it in sight, you can do the following in your controller (I have not tested it):

using MyRefinements

helper: MyRefinements OR helper_method: MyRefinements

0
source share

All Articles