Reputation: 9573
I'm using the will_paginate gem in my Rails app and loop through an array of model instances which are rendered on the page and limited to 5 per page. Before I added will_paginate I numbered each item with a simple <% i += 1 %> which of course went +1 with each loop through the array, and worked fine.
But now that I'm using will_paginate the count restarts on each page, so page 1 items go 1, 2, 3, 4, 5, and then on the second page it starts over... 1, 2, 3, 4, 5
Obviously this isn't ideal. How do I get the count to continue as you go to previous pages?
Upvotes: 1
Views: 928
Reputation: 106882
A will_paginate
Collection
is not just a simple Array
or ActiveRecord::Relation
. Instead it has some additional methods defined, for example: current_page
, per_page
, offset
, total_entries
or total_pages
.
You can use Collection#offset
to calculate your current index:
<% @collection.each_with_index do |item, index| %>
<%= @collection.offset + index + 1 %>
...
<% end %>
What can be simplified by initializing Enumerator#with_index
with the first index (note the .
between each
and with_index
:
<% @collection.each.with_index(@collection.offset + 1) do |item, index| %>
<%= index %>
...
<% end %>
Upvotes: 5