Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Where to define a helper method that will be available to several controllers?

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 ?

like image 208
Misha Moroshko Avatar asked Jan 19 '11 10:01

Misha Moroshko


People also ask

Can we use helper method in controller Rails?

In Rails 5, by using the new instance level helpers method in the controller, we can access helper methods in controllers.

Can helper methods be used to transfer data from controller to view?

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.


1 Answers

It should be in app/controllers/application_controller.rb

The app/helpers/application_helper.rb is for shared view helpers.

like image 83
Satya Avatar answered Nov 14 '22 20:11

Satya