Pagination: getting item/attributes from prev/next page


#1

Hi all,

Is it possible to get the item(s) from previous or next pages in a
Paginator?

I have a 1-item-per-page Paginator, and I need to access the
attributes of the object in the previous and next pages.

Let me give my code to better illustrate the situation. In my
controller I have this:

def photo

@album = Album.find(:first,
:conditions => [‘slug = ?’, params[:albumslug]])
@photo = Photo.find(:first,
:conditions => [‘album_id = ? and slug = ?’,
@album.id, params[:photoslug]])
@photo_pages, @photos =
paginate :photos,
:order_by => “order_rank asc”,
:per_page => 1,
:conditions => [“status=‘published’ and album_id = ?”,
@album.id]
@prevphoto = ??? # what to put here?
@nextphoto = ??? # what to put here?

end

And in my view photo.rhtml I have something like this:


<% if @prevphoto %>
<%= link_to image_tag(@prevphoto.src_uri,
:alt => h(@prevphoto.title),
:title => h(@prevphoto.title)),
photo_url(:albumslug => @prevphoto.album.slug,
:photoslug => @prevphoto.slug) %>
<% end %>

And similarly for @nextphoto.

Thanks in advance.

Cheers,
Ronny


#2

On Nov 7, 2005, at 11:40 PM, Ronny H. wrote:

   :per_page => 1,

<% if @prevphoto %>
<%= link_to image_tag(@prevphoto.src_uri,
:alt => h(@prevphoto.title),
:title => h(@prevphoto.title)),
photo_url(:albumslug => @prevphoto.album.slug,
:photoslug => @prevphoto.slug) %>
<% end %>

Ronny-

Just an idea but why don't you paginate by 3 items and then just

show the middle one? This way you have access to the previous and
next items in the pagination but you only need to show the middle one
so it looks like you are paging by one. This is the simplest way I
can think to solve this issue.

HTH

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#3

On Tue, Nov 08, 2005 at 08:30:13AM -0800, Ezra Z. wrote:

show the middle one? This way you have access to the previous and
next items in the pagination but you only need to show the middle one
so it looks like you are paging by one. This is the simplest way I
can think to solve this issue.

G’day Ezra,

I had actually thought about paginating by 3 items, but it’s not the
most natural/intuitive way, I’m afraid it would make code maintenance
a wee bit more difficult in the future.

Having said that, paginating by 3 items should work fine. Need to
handle first and last pages as they are special cases. The middle
pages should be easy enough to figure out.

I will do it this way for now. I’m still open for other suggestions,
though.

Thank you for your help.

Ronny