Forum: Ruby on Rails no helper methods in controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D41d8cd98f00b204e9800998ecf8427e?d=identicon&s=25 Martin (Guest)
on 2005-11-16 14:03
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
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 jarkko (Guest)
on 2005-11-16 14:42
(Received via mailing list)
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
2585e5de3d03793e39077e3d8b73dfaf?d=identicon&s=25 ronnylist (Guest)
on 2005-11-16 14:45
(Received via mailing list)
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
D41d8cd98f00b204e9800998ecf8427e?d=identicon&s=25 Guest (Guest)
on 2005-11-16 14:55
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
2585e5de3d03793e39077e3d8b73dfaf?d=identicon&s=25 ronnylist (Guest)
on 2005-11-16 15:00
(Received via mailing list)
On Wed, Nov 16, 2005 at 03:42:26PM +0200, Jarkko Laine 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/ActionControlle...
probably needs to be updated. (Or does it reflect 1.0?)

'helper :something' doesn't work, 'include SomeHelper' works.

Ronny
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 jarkko (Guest)
on 2005-11-16 16:08
(Received via mailing list)
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
This topic is locked and can not be replied to.