I have defined my helper function in Helper:
module CarsHelper
def my_helper
...
end
end
But I can neither used it(my_helper) in my CarsController
nor in Car
model, is it so that the custom helper can only be used in View?
Helpers are normally for views. But you can include them in your controllers as well. Just add
helper :cars
to your controller. (docs)
Models are out of scope for helpers. Use class or instance methods in there instead.
The new way to use view helpers in controllers is to use:
helpers.[helpername]
See this for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With