Helper versus Partial

hi all,

Im looking for some pointers on best practices of when to use a helper
and when to use a partial.

first, helpers…

for instance, i need to be able to display a sentence such as

“There are X things in your queue”

where X is an integer

now, depending on the number of things, the grammar will be different.
if there is only 1 thing, I need it to say “There is 1 thing…”,
with 2 or more, it needs to read “There are 2 things…” and with 0
things, “There is nothing…”

now i could put all the code to handle this in my template, but that
would be ugly (and not DRY), or i could but in a helper and just call
the helper when I need to display that sentence correctly

def queue_title(things = 0)
title = “There "
case things
when 0
title += “is nothing”
when 1
title += “is 1 thing”
else
title += “are #{things} things”
end
title += " in your queue”
title
end

then in my view template

<%= title_for_queue(@things.size) ->

now a partial is a chunk of your view template that you want to either
reuse or use repeatedly. candiates for partials are table rows, list
items, forms, etc.

so say i have a form that i want to use a few different views, i would
put it into a partial

_my_form.rhtml

...

and wherever i want to show this form i can call

<%= render :partial => “my_form” -%>

i suggest you also read the rails docs on helpers and partials.

hope this helps

Chris

My rule of thumb is that if the rhtml is way to full of ERb <% %> tags,
I’ll make a helper instead.