Don't Assign Variables in Templates October 5th, 2007

You have an array whose size is unknown. Render all elements less than or equal to 29.

The ugly version:

<%- upper_limit = refinements.length >= 29 ? 29 : refinements.length -%>
<%- 0.upto upper_limit-1 do |index| -%>
  <%- refinement = refinements[index] -%>
  <%= refinement_formatting refinement -%>
<%- end -%>

If you're assigning variables inside ERB templates, you're probably doing something wrong.

Here's the clean version:

<%- refinements[0..28].to_a.each do |refinement| -%>
  <%= refinement_formatting refinement -%>
<%- end %>

Ruby won't complain if you call a range outsize the array's size. It will return a nil, hence the call to "to_a."