Ronny Haryanto
2005-Nov-08 07:40 UTC
Pagination: getting item/attributes from prev/next page
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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Zygmuntowicz
2005-Nov-08 16:30 UTC
Re: Pagination: getting item/attributes from prev/next page
On Nov 7, 2005, at 11:40 PM, Ronny Haryanto wrote:> 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 %> > ... >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-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
Ronny Haryanto
2005-Nov-09 05:42 UTC
Re: Pagination: getting item/attributes from prev/next page
On Tue, Nov 08, 2005 at 08:30:13AM -0800, Ezra Zygmuntowicz wrote:> On Nov 7, 2005, at 11:40 PM, Ronny Haryanto wrote: > >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. > > 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.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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails