Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The scope of custom helper

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?

like image 316
Mellon Avatar asked Jan 25 '11 12:01

Mellon


2 Answers

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.

like image 167
edgerunner Avatar answered Sep 24 '22 13:09

edgerunner


The new way to use view helpers in controllers is to use: helpers.[helpername]

See this for more.

like image 37
MIA Avatar answered Sep 26 '22 13:09

MIA