How can I access a controller method from a helper?

Hi,

everything is in the title… I’ve got a function declared in my
controller… I want to access it from a helper. I copied/pasted the
function in the controller and the helper and it works… But as Rails
is “DRY”, I’d like to find a better way…

Thanks.
Nicolas.

It sounds like the method you want to call isn’t actually a public
method?
Otherwise calling it from a helper wouldn’t make sense. If it’s a
“helper”
method you want to call, then I’d suggest moving it into lib/.

You’ll have to find another place to put the function, or make an Ajax
call
to that controller action when the page loads.

A suggestion: if it’s tied to the model you’re using, put the function
in
the model.

Jason

On 8/28/06, Nicolas B. [email protected] wrote:

everything is in the title… I’ve got a function declared in my
controller… I want to access it from a helper. I copied/pasted the
function in the controller and the helper and it works… But as Rails
is “DRY”, I’d like to find a better way…

In your controller, helper_method :mymethod

See
http://api.rubyonrails.com/classes/ActionController/Helpers/ClassMethods.html#M000139

jeremy

What about if you put it into the model (presuming it does not have
controller or view specific code). Maybe you can elaborate a little
more on the purpose of your function

kind regards

Matt


www.matthiasvonrohr.ch

Ah yes, another good suggestion. If it’s a method used in many places,
it
may belong in another class entirely and can reside in lib/

Jason