I would like to define a helper method, my_method
, that will be available inside BuyersController
methods (like index
, create
, e.t.c.).
I tried to define it in app/helpers/application_helper.rb
but it didn't work:
undefined method `my_method' for #<BuyersController:0x26df468>
It should be in some shared place because I want to use it in other controllers also. This is why I tried app/helpers/application_helper.rb
.
What is the right place to define it ?
In Rails 5, by using the new instance level helpers method in the controller, we can access helper methods in controllers.
You can put a method in a controller and then call helper_method in the controller to indicate that this method is available as if it were in a helper too. This method will then be available to all controllers and to all views.
It should be in app/controllers/application_controller.rb
The app/helpers/application_helper.rb
is for shared view helpers.
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