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
on 2007-03-02 23:15
on 2007-03-02 23:25
Hey Paul, 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
on 2007-03-02 23:32
> 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 !!Es gratis!!
on 2007-03-02 23:40
The first approach does just what I wanted. Thanks a lot for your help!