I've got a list of items I'm looping in an each in my show view. This is all perfectly fine. However, I would like to get a number infront of each item which increments with every loop ( i = 0, i++ you know the drill ).
Now, how can i do this in Rails? This is what I get by now:
<% i = 0 %>
<% @trip.triplocations.each do |tl| %>
<article id="<%= dom_id(tl) %>">
<strong> <%= tl.title %> </strong>
<p>
<%= tl.description %>
</p>
</article>
<% end %>
Use #each_with_index instead of instantiating a variable in view!
<% @trip.triplocations.each_with_index do |tl, i| %>
<article id="<%= dom_id(tl) %>">
<strong> <%= i %>. <%= tl.title %> </strong>
<p>
<%= tl.description %>
</p>
</article>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With