Forum: Ruby on Rails Documentation for Writing Helpers?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
7b74ccf93dde7dd2ac5d2980d14fdc7b?d=identicon&s=25 Paul Lynch (plynchnlm)
on 2007-03-02 23:15
(Received via mailing list)
Is there documentation somewhere for how to write a helper?  I
understand that I'm supposed to define a method in the *_helper.rb
method associated with my view, but I'm not sure how to output stuff
to the page.  (I tried puts, but that didn't work.)  If there isn't
documentation, are there examples someplace that I should be looking
at?  Thanks,
8f3739e3b34bfea19fd381500d8a8fe6?d=identicon&s=25 Anthony Carlos (acarlos)
on 2007-03-02 23:25
(Received via mailing list)
Hey Paul,

It's not all that explicit, so here are some key points.

1. Helpers are helpers are defined in modules.
2. The modules are mixed into the controller either automatically by
naming convention or you can add your own.
3. To use a helper, just call its method name in the view. In the
docs, they call the method hello_world from the view as: <%=
hello_world %>

Hope it helps,

1f2eadfb41362800ebc2cf211b91d0f7?d=identicon&s=25 javier ramirez (Guest)
on 2007-03-02 23:32
(Received via mailing list)
> method associated with my view, but I'm not sure how to output stuff
> to the page.  (I tried puts, but that didn't work.)
well.. from a helper the usual way of rendering things into a view is
accumulating the output on a variable and then just return it at the end
of the helper. Like this

    def my_useless_helper
       output << do_whatever

And then in the view you can just use it like

If that's not enough for you (usually it is) there is one way to send
text to the output of the erb directly. The method is call concat (you
can look it up at the rails api). The tricky thing here is that you must
have a binding for outputting like that. You can work around that by
passing a block to the helper and then getting the binding of the block
like this

    def my_useless_but_more_sophisticated_helper(&block)
       concat 'Write this directly to the erb', block.binding unless

I only use this kind of thing when I want to manipulate the body of the
block I'm passing (suppose I want to parse the html inside the block and
do soething with it). For evey day use, the former is much easier and
you'll see it much more often.


javier ramirez

Estamos de estreno... si necesitas llevar el control de tus gastos
visita !!Es gratis!!
7b74ccf93dde7dd2ac5d2980d14fdc7b?d=identicon&s=25 Paul Lynch (plynchnlm)
on 2007-03-02 23:40
(Received via mailing list)
The first approach does just what I wanted.   Thanks a lot for your
This topic is locked and can not be replied to.