How do I use Ruby refinements in Rails views?
Asked Answered
A

2

10

I have a Rails 4 project using Ruby 2.0. I've defined some refinements. Putting

<% using MyRefinements %>

at the top of the view files causes the error "undefined method '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 method' error if I try to use it in the view.

Thanks!

Andres answered 17/3, 2014 at 19:27 Comment(1)
Not very elaborate opinion...Overlying
A
3

Unfortunately, this does not seem possible. For posterity, I'm documenting the things I tried that didn't work:

In the view:

<% using MyRefinements %>

In the controller (each tried separately):

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

In the helper:

using MyRefinements

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

Too bad.

Apuleius answered 5/5, 2016 at 22:4 Comment(1)
This seems odd to me. Is there still no way to use refinements in the view?Earleanearleen
S
1

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

using MyRefinements

helper :MyRefinements OR helper_method :MyRefinements

Symer answered 5/12, 2014 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.