# Documentation for Writing Helpers?

**URL:** <https://www.ruby-forum.com/t/documentation-for-writing-helpers/87486>\
**Category:** Rails\
**Created:** [March 2, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/documentation-for-writing-helpers/87486 "2007-03-02T23:15:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [March 2, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/documentation-for-writing-helpers/87486/1 "2007-03-02T23:15:42Z")

</div>

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,  
–Paul

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [March 2, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/documentation-for-writing-helpers/87486/2 "2007-03-02T23:25:43Z")

</div>

Hey Paul,

[http://api.rubyonrails.org/classes/ActionController/Helpers/](http://api.rubyonrails.org/classes/ActionController/Helpers/)  
ClassMethods.html

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,

-Anthony

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [March 2, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/documentation-for-writing-helpers/87486/3 "2007-03-02T23:32:45Z")

</div>

> 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=String.new
   output << do_whatever
   output
end

```

And then in the view you can just use it like  
\<%=my\_useless\_helper%\>

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

```

!block\_given?  
end

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.

Regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![plambert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@plambert](https://www.ruby-forum.com/u/plambert)\
**Post date:** [March 2, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/documentation-for-writing-helpers/87486/4 "2007-03-02T23:40:46Z")

</div>

The first approach does just what I wanted. Thanks a lot for your  
help!
