Call an helper method from a controller

Hello, this have been debated few times in the forum but I didn’t found
a satisfying solution : my problem is very simple, I have few methods
dealing with cookies that are intended to be used in controllers and
views.

Let say I have the read_cookie() method. Because it deals with the
response object putting this method in the /lib directory doesn’t fit
nicely because I would need to pass the object method to each call to
read_cookie, as in read_cookie(response)

If I put read_cookie in a application helper, I can’t use it in the
controller and if I put it in the application.rb file I can’t use it
from the view

So, I’m quite sure there is an elegant solution provided by RoR as
usual… But which one ?

Thanks

nuno wrote:

Hello, this have been debated few times in the forum but I didn’t found
a satisfying solution : my problem is very simple, I have few methods
dealing with cookies that are intended to be used in controllers and
views.

Let say I have the read_cookie() method. Because it deals with the
response object putting this method in the /lib directory doesn’t fit
nicely because I would need to pass the object method to each call to
read_cookie, as in read_cookie(response)

If I put read_cookie in a application helper, I can’t use it in the
controller and if I put it in the application.rb file I can’t use it
from the view

So, I’m quite sure there is an elegant solution provided by RoR as
usual… But which one ?

Thanks

I mean “I will need to pass the response object to every call to
read_cookie or write_cookie” (in the case I use /lib)

Still no idea

You can also directly instanciate a Helper object and call a method.

example with number_to_currency:

Object.new.extend(ActionView::Helpers::NumberHelper).number_to_currency(number,
options)

but beware that is there are methods of other Helpers methods, they will
fail.

define the helper in the controller, and use helper_method maybe?

http://groups-beta.google.com/group/rubyonrails-talk/browse_thread/thread/e31977720617b676/8b5dd9e5209e3b3e?lnk=gst&q=view+controller+helper&rnum=1#8b5dd9e5209e3b3e

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs