In Rails, is there any way to use the app / helpers / foo.rb methods in Controller vs in View?

It seems that these helpers in app / helpers / foo.rb can be used in the view but cannot be used in the controller?

In some cases, different controllers may have to use the same method, but just pass a different parameter, so in this case it makes no sense to use an auxiliary element in the controller?

+3
source share
3 answers

There are two main ways to reuse code between controllers.

ApplicationController, , , . ( ApplicationController , .)

( ) , .

- , , - , , .

+3

, . ( - , , )

ApplicationController, .

+1

As mentioned above, you can put common helpers in an ApplicationController or subclass. I would add that in order to make them viewable, you added to the top of the class:

helper_method :foo
+1
source

Source: https://habr.com/ru/post/1764981/


All Articles