Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting in View Rails

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 %>
like image 692
CaptainCarl Avatar asked Oct 13 '12 06:10

CaptainCarl


1 Answers

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 %>
like image 95
waldyr.ar Avatar answered Oct 18 '22 15:10

waldyr.ar