Patterns of use for helpers and partials


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?


Here’s something to think about:

Remember that you can apply a partial to a collection?

Assuming this partial: _product.rhtml

<%=h( %>
<%=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.