# Help with prev/next links

**URL:** <https://www.ruby-forum.com/t/help-with-prev-next-links/194058>\
**Category:** Rails\
**Created:** [August 29, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/help-with-prev-next-links/194058 "2010-08-29T14:53:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaspir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ab097/32.png) [@kaspir](https://www.ruby-forum.com/u/kaspir)\
**Post date:** [August 29, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/help-with-prev-next-links/194058/1 "2010-08-29T14:53:45Z")

</div>

I’ve written a Image Gallery app in rails. It contains photo albums, in  
which you can upload photos, then click on an individual photo to view  
it’s larger size. When you bring up the larger size, I have previous and  
next buttons to go through the photos in the album. My problem is when  
navigating through photos via the next and previous buttons, it scrolls  
through ALL photos instead of photos only in that album.

Previous and Next buttons in picture.html.erb

 \<%= link\_to("Previous", @photo.previous\_photo) if @photo.previous\_photo %\> \<%= link\_to("Next", @photo.next\_photo) if @photo.next\_photo %\> 

Methods in model

def previous\_photo  
self.class.first(:conditions =\> [“id \< ?”, id], :order =\> “id desc”)  
end

def next\_photo  
self.class.first(:conditions =\> [“id \> ?”, id], :order =\> “id asc”)  
end

Method in Albums Controller

def picture  
@photo = Photo.find(params[:id])  
end

I tried doing it like this:

\<%= link\_to(“Previous”, @album.photo.previous\_photo) if  
@album.photo.previous\_photo %\>

def picture  
@album = Album.find(params[:id])  
@photo = Photo.find(params[:id])  
end

With this result:

Couldn’t find Album with ID=25
