Patterns of use for helpers and partials

Hello,

Are there any guidelines as to when helpers or partials are a better
option. If there is a lot of markup we are using partials, otherwise
it has been up to the developer.

Is there an established best practice?

Thanks,
Dan

Dan M. wrote:

Hello,

Are there any guidelines as to when helpers or partials are a better
option. If there is a lot of markup we are using partials, otherwise
it has been up to the developer.

Is there an established best practice?

Thanks,
Dan

Dan,
I’ve noticed something about forums like this. Anybody can write almost
anything in their original post, and it may go unnoticed, but as soon as
somebody messes up in a reply, people are all over it.

For that reason, even though I’m a newby, I often reply to things that
interest me just to bring the “group elders” out of hiding and get the
real answer. To that end, I offer the following…

I tend to think of helpers as mostly eRB code, and partials as mostly
html. Need to format a date or have a loop that fixes up some tabs or
something, helper time. Have a form that gets shown/hidden via ajax,
that’s a partial.

Partials are great for ajax views where you have lots of stuff happening
in the same view, but not all at once. There can be so much stuff there
that it gets really cluttered. Partials are a great way to make the
view into a sort of skeleton or outline of the page, and put all the
gory details off in the partials.

jp

Here’s something to think about:

Remember that you can apply a partial to a collection?

Assuming this partial: _product.rhtml

<%=h(product.name) %>
<%=h(product.description) %>
<%=number_to_currency(product.price) %>

This would render that partial once for each entry in the @products
collection:

<%=render :partial=>“product”, :collection=>@products %>

What does the iteration? the helper “render”. This is a prime example
of
how you can use helpers in Rails.

Things like number_to_currency, h, link_to, link_to_unless_current,
image_tag, text_field, etc… are all helpers. They’re methods that
produce
output. Helpers don’t always exist in the view… they’re just methods
in a
module.

How you use them is up to you, but the idea is supposed to be that
helpers
keep your logic out of the view, and partials provide an easy mechanism
for
reuse.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs