Search code examples
ruby-on-railsruby-on-rails-4helperpartial

Rails - Use a Helper or a Partial


In my Article model, I have 4 different columns: short_effects, long_effects, benefits, and alternatives. They are all text columns.

In my view, I am calling each one in the same basic format:

<% unless @article.short_effects.blank? %>
  <hr id="effects">
  <h2><span class="glyphicon glyphicon-hourglass"></span>Short-term side effects</h2>
  <ul>
    <% @article.short_effects.split(';').each do |effect| %>
      <li><%= effect.downcase %></li>
    <% end %>
  </ul>
<% end %>

<% unless @article.long_effects.blank? %>
  <hr id="effects2">
  <h2><span class="glyphicon glyphicon-time"></span>Long-term side effects</h2>
  <ul>
    <% @article.long_effects.split(';').each do |effect| %>
      <li><%= effect.downcase %></li>
    <% end %>
  </ul>
<% end %>

.
.
.

to eliminate repetition, I would like to create a helper or a partial... however, I do not know which is better for this situation. I know some people frown upon HTML inside of helpers, but since helpers are used to reduce code from views, I feel that may be the best way. Any advice is greatly appreciated.


Solution

  • There are some existing discussions about this one here. In particular, this question has a variety of (mostly short) answers with some good rules-of-thumb you might consider. I would personally be of the "minimize HTML in a helper" school of thought, however I also like the description of helpers being for specific elements while using partials for larger chunks of code.