Helper_method controller

I was wondering why someone should use helper_method inside the controller to create a helper method instead of creating a “normal” method, which is inside the helper file. What are the pros and cons of this?

+42
ruby-on-rails ruby-on-rails-3
Aug 23 '10 at 16:43
source share
2 answers

helper_method is useful when functionality is what is used both between the controller and the view. A good example is current_user .

If the method is more related to the logic of the controller rather than formatting, then it belongs to the controller. Something like current_user will be shared among all controllers, so it must be defined in ApplicationController .

The true “helper” methods relate to the view and handle things like formatting and template logic. They are rarely needed in the controller, and they belong to their own module under the application / helpers. You can include them in your controller when necessary, but in the end you will get a class support method available for your controller available to your controller.

+81
Aug 23 '10 at 16:46
source share

To exchange methods between the controller and the view, you have several options:

+14
Jun 17 '11 at 16:03
source share



All Articles