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?
A
will_paginateCollectionis not just a simpleArrayorActiveRecord::Relation. Instead it has some additional methods defined, for example:current_page,per_page,offset,total_entriesortotal_pages.You can use
Collection#offsetto calculate your current index:What can be simplified by initializing
Enumerator#with_indexwith the first index (note the.betweeneachandwith_index: