No helper methods in controller


#1

Hi

I’m on rails version 0.14.3.
The methods of the controller helper module are not available in the
controller instance. They are available on the view. Verified it with a
plain new project.

May this be a bug or am I missing something?

thanks a lot in advance

Martin


#2

On 16.11.2005, at 15.03, Martin wrote:

Hi

I’m on rails version 0.14.3.
The methods of the controller helper module are not available in the
controller instance. They are available on the view. Verified it
with a
plain new project.

May this be a bug or am I missing something?

No, that’s like it’s supposed to be. Helpers are methods meant to
make views cleaner by encapsulating common behaviour in one place.
They are thus not available in controllers.

You can, however, include them in your controllers, as well:

include DateHelper

You might consider, however, whether your code is in the right place
in helpers if you need a lot of it often in controllers in contrast
to views.

//jarkko


#3

On Wed, Nov 16, 2005 at 02:03:20PM +0100, Martin wrote:

I’m on rails version 0.14.3.
The methods of the controller helper module are not available in the
controller instance. They are available on the view. Verified it with a
plain new project.

May this be a bug or am I missing something?

I can confirm it here as well. Don’t know what’s going on, though.

Ronny


#4

jarkko wrote:

May this be a bug or am I missing something?
No, that’s like it’s supposed to be. Helpers are methods meant to
make views cleaner by encapsulating common behaviour in one place.
They are thus not available in controllers.

You can, however, include them in your controllers, as well:

include DateHelper
That makes sense, and by using “include” it works (standard ruby stuff).
But if this is true there are some serious “errors” in the Rails book.

From page 554:

By default, each controller gets its own helper module. It wonâ??t be
surpris-
ing to learn that Rails makes certain assumptions to help link the
helpers
into the controller and its views. If a controller is named
BlogController,
it will automatically look for a helper module called BlogHelper in the
file
blog_helper.rb in the app/helpers directory.

and there is even an example with

class ParticularController < ApplicationController
helper :date_format


and rails is not complaining when I am using “helper” but it does not
work.

?

Martin


#5

On Wed, Nov 16, 2005 at 03:42:26PM +0200, Jarkko L. wrote:

No, that’s like it’s supposed to be. Helpers are methods meant to
make views cleaner by encapsulating common behaviour in one place.
They are thus not available in controllers.

You can, however, include them in your controllers, as well:

include DateHelper

Hmm, the API docs at
http://api.rubyonrails.com/classes/ActionController/Helpers/ClassMethods.html
probably needs to be updated. (Or does it reflect 1.0?)

‘helper :something’ doesn’t work, ‘include SomeHelper’ works.

Ronny


#6

Ronny and Martin,

On 16.11.2005, at 15.55, Guest wrote:

into the controller and its views. If a controller is named


and rails is not complaining when I am using “helper” but it does not
work.

Using “helper :myhelper” will make the helper available in the
views
of that particular controller.

So helper is a perfectly working method but it’s not meant to do what
you think it is.

Also, like Torben mentioned, the api docs at least seem to be fine.
The fact that the helpers are not available in the controller code
could perhaps be stated clearly, tho. Do I hear someone volunteering
for a documentation patch?

//jarkko