Forum: Ruby on Rails Helper or partial?

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.
Per D. (Guest)
on 2005-11-13 13:01
I often find myself not being able to choose if I should use a partial
or a helper. They both get the job done but I want to know more about
why it's better to use one or the other in certain situations etc.

What do you use, and when?
petermichaux (Guest)
on 2005-11-13 19:50
(Received via mailing list)
When I first saw helpers in the Todo list tutorial I thought this is
ugly solution

def display_items(ary)
result_string = ""
ary.each do |@item|
result_string << check_box("item", "done", "onclick" => "
document.location.href='/todo/toggle_check/#{}'") + " "
result_string << @item.description + " "
result_string << link_to("Edit", :action => "edit", :id =><>)
+ " "
result_string << link_to("Destroy",
{ :action => "destroy", :id => <> },
:confirm => "Are you sure you want to delete this entry:
result_string << "<BR />"
return result_string

Having all the 'result_string = ' and the '+" "' is not very appealing.

- - - - - - -

In the very next section of the same Todo list tutorial the solutions
partials is much more appealing. It looks just like a template.

<%= check_box("item", "done", "onclick" => "
document.location.href='/todo/toggle_check/#{}'") %>
<%= @item.description %>
<%= link_to("Edit", :action => "edit", :id => <>)
<%= link_to("Destroy", { :action => "destroy", :id =><>},
:confirm => "Are you sure you want to delete this entry:

- - - - - - - -

I don't think I would include things like check_box() or link_to() in a
helper. I would use a helper for something very small like the format
dollars helper in the Rails book p98.

module ApplicationHelper
def fmt_dollars(amt)

In this helper there is no appending to a string and no other helpers.
just does a tiny task. The simple call in the template <%=
fmt_dollars(%product.price) %> also indicates this is a method as
opposed to
a big chunk of partial template being inserted <%= render_partial
@item %>.

- Peter
steven.mohapibanks (Guest)
on 2005-11-13 20:02
(Received via mailing list)
My very straightforward rule of thumb is this; use partials, and if
they start to get horribly cluttered with lots of logic, change them
to helpers. I also sometimes mix the two, so use helpers to contain
logic and call out to partials to handle the actual rendering.

It's all a style preference - I don't like my templates/partials full
of code as they just get unreadable and horrible to maintain. (Guest)
on 2005-11-14 00:02
(Received via mailing list)
Dumb newbie question: Is it possible to output content from a helper
using something like 'h', or do you have to build up the string and
return it.

I'd been presuming you have to use a string because the helper might
not be view related.
This topic is locked and can not be replied to.