Forum: Ruby on Rails Pagination: getting item/attributes from prev/next page

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ronnylist (Guest)
on 2005-11-12 12:49
(Received via mailing list)
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
ezra (Guest)
on 2005-11-12 12:49
(Received via mailing list)
On Nov 7, 2005, at 11:40 PM, Ronny Haryanto 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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
ronnylist (Guest)
on 2005-11-12 12:49
(Received via mailing list)
On Tue, Nov 08, 2005 at 08:30:13AM -0800, Ezra Zygmuntowicz 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
This topic is locked and can not be replied to.