Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: Call functions inside controllers

If I want to have functions to be called inside controllers, where should I put them?

like image 241
donald Avatar asked Jan 05 '11 15:01

donald


1 Answers

if you want it to be local to a controller then all you need to do is to add it to the controller you wish to use.

private
def myfunction
  function code.....
end

to all controllers you can put it inside the application controller, because all controlers are sub classed.

ApplicationController

protected

def myfunction

  function code.....

end

If you want access in your views then you can create a helper

ApplicationHelper

def myfunction

  function code...

end
like image 189
ddayan Avatar answered Sep 18 '22 20:09

ddayan