Getting the current item number or index when using will_paginate in rails app

Posted by Rich on Stack Overflow See other posts from Stack Overflow or by Rich
Published on 2010-12-30T22:04:51Z Indexed on 2010/12/30 22:54 UTC
Read the original article Hit count: 140

Filed under:
|

I have a rails app that stores movies watched, books read, etc. The index page for each type lists paged collections of all its items, using will_paginate to bring back 50 items per page. When I output the items I want to display a number to indicate what item in the total collection it is. The numbering should be reversed as the collection is displayed with most recent first. This might not relate to will_paginate but rather some other method of calculation.

I will be using the same ordering in multiple types so it will need to be reusable. As an example, say I have 51 movies. The first item of the first page should display:

  1. Fight Club - Watched: 30th Dec 2010

Whilst the last item on the page should display:

  1. The Matrix - Watched: 3rd Jan 2010

The paged collection is available as an instance variable e.g. @movies, and @movies.count will display the number of items in the paged collection. So if we're on page 1, movies.count == 50, whilst on page 2 @movies.count == 1. Using Movie.count would give 51.

If the page number and page size can be accessed the number could be calculated so how can they be returned? Though I'm hopeful there is something that already exists to handle this calculation!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about pagination