1

I asked this same question about a week ago, but I might not have been clear with what I wanted. I already have a table created that is sorted/filtered by several different select boxes. I need the first column in my table to increment, for example:

1 |

2 |

3 |

etc.

I know that this is a simple problem that has an easy explanation, but I have not been able to figure it out. I would like it to function something like the teams in this website: http://espn.go.com/college-football/rankings

Any help is greatly appreciated

Thanks,

Jack

jackadanos
  • 133
  • 2
  • 12

1 Answers1

3

If you just want number in a table that always run consecutively (1,2,3), then just do:

<% count = 1 %>
<table>
  <% collection.each do |c| %>
    <tr>
      <td><%= count %></td>
      <td>other info...</td>
    </tr>
    <% count += 1 %>
  <% end %>
</table>

Or, even shorter (as suggested in the comments by "@mu is too short")

<table>
  <% collection.each_with_index do |item, index| %>
    <tr>
      <td><%= index %></td>
      <td><%= item </td>
    </tr>
  <% end %>
</table>
Carson Cole
  • 4,183
  • 6
  • 25
  • 35
  • where would I put the count = 1 @carson – jackadanos Oct 20 '12 at 20:20
  • edited my example fixing the count variable. Just put the variable before the loop that iterates the rows of your tables. The incrementer (count +=1) should be inside the loop. – Carson Cole Oct 20 '12 at 21:00
  • I will try this soon and let you know how it works! Thanks! @carson – jackadanos Oct 20 '12 at 23:04
  • 1
    Thank you this is great! The only problem is that when I change pages it resets the count. I am using will_paginate. Do you know how I could fix this? Thanks! @carson – jackadanos Oct 20 '12 at 23:55