views:

783

answers:

2

I have some photos that are split on successive pages through will_paginate plugin. When you open a photo and then return to all photos using a link, you always return to the first page (e.g. the photo is displayed on page 5, you open the photo, click a link to show all photos and expect that you are on page 5 again, but you are on page 1).

Now, is there any method to get the page number to which a photo belongs to?

I tried to pass a GET parameter, but this only works if the user doesn't perform any more actions (e.g. post a comment, edit photo, ecc.).

A: 

The page number is highly dependent on the results of your search. Your search might have 1, 10, or 100 pages depending on the result set and the number of items per page.

Any link to "show all photos" could contain the search and pagination information, using the GET parameters as you've described. Or store and retrieve via a cookie so that the search results persist until the users clears or selects a new search.

Terry Lorber
The problem is that when the user temporary leaves the photo page (for example to modify it) and then tries to return back to show all photos he always returns to the first page :(While I don't think using cookies it is a good thing for SEO.
collimarco
@collimarco If you want to reuse search and paginations parameters between requests, then you'll need to persist them somewhere. That's usually what cookies are for. Not sure what impact this would have on SEO.
Terry Lorber
I saw that many photography sites (such as deviantart.com) use cookies to allow users to go back.
collimarco
+2  A: 
page = (number_of_records_before_RECORD / number_of_pages) + 1

In other words. If you photo has ID 40 and there are 25 records before (assuming some records has been deleted), with 20 records per page:

page = (25 / 20) + 1 = 2

You can count the number of records before selected record using Model.count(:conditions => ['id < ?', record.id], :order => 'id'). The right query depends on which sorting filter you apply to that table when listing all objects.

Simone Carletti
This may be a solution, but I wonder why this code isn't implemented by default in the plugin. Maybe because it doesn't make sense to do what I said in terms of performance..
collimarco
Because it doesn't really make sense for a broad audience. This isn't a perfect solution but just a workaround for your specific need. Also, will_paginate provide pagination facilities, not additional ActiveRecord functionalities.
Simone Carletti
I wouldn't have thought it would be a huge performance hit. When you're using a pagination library it will count all of the things you are paging to get the total page count. Provided the column you are ordering on is indexed correctly then it shouldn't be too painful to get a similar count to find the current page.
Shadwell
It's also worth, if you can, doing the count to find the current page in the same transaction as the query to find the items in the page - otherwise a user may find themselves at a page which doesn't have the photo they were expecting on because the number of photos has changed between queries.
Shadwell
I agree wutg Shadwell, this is the reason why I commented "This isn't a perfect solution but just a workaround for your specific need" and one of the best reason why this doesn't really make sense in will_paginate. If you want such this feature, you should code it in the way it best fits your application and requirements.
Simone Carletti
Practically you said to pass back to the gallery something like a GET parameter containing the ID of the photo from which the user comes from, and from that retrieve the current page. Or maybe you said to use a cookie to store the ID of the photo along with the search query? I can't really understand what you suggested..
collimarco
You don't need to pass the ID neither via GET nor via Cookie because you know that value: it's the ID of the current record.
Simone Carletti
So you simply said to determine the item's page number when I display the single photo. Ok, this is a solution.. but I still need to keep trace of the search query otherwise I won't be able to determine the correct page. Where is the best way to store it?
collimarco