# How to implement go to next / previous item?

**URL:** <https://www.ruby-forum.com/t/how-to-implement-go-to-next-previous-item/121768>\
**Category:** Rails\
**Created:** [November 30, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-implement-go-to-next-previous-item/121768 "2007-11-30T19:30:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [November 30, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-implement-go-to-next-previous-item/121768/1 "2007-11-30T19:30:16Z")

</div>

I have an array of posts, which could be list of all posts, or posts  
of certain user, or posts from a tag search. I present it as a  
paginated list, with title and brief summary. when the user selects a  
post, I show the whole thing on next page.

Right now, I just have a back link to the list, but I’d like to  
implement go to next / prev post in list. This is tricky because the  
list is gone, of course, and it wouldn’t be easy to save it, and not  
easy to calculate for each post b/c it’s not just “find the next/prev  
one in the posts table”

I’m using the google interface and they don’t have this implemented  
either. the nexy/prev actually goes to the next/prev article in the  
entire group and not, for example, in the search result.

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [November 30, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/how-to-implement-go-to-next-previous-item/121768/2 "2007-11-30T19:56:07Z")

</div>

> I’m using the google interface and they don’t have this implemented  
> either. the nexy/prev actually goes to the next/prev article in the  
> entire group and not, for example, in the search result.

On the initial list page setup a global javascript array (or put it in a  
cookie or something else persistent) that contains a list of all the  
id’s  
of the posts themselves (in the right order).

Then on an individual post page you can get the prev/next posts by  
finding  
the current id in that array and looking at the array element just  
before  
and just after it.

-philip

---

<div class="post-metadata">

**Author:** ![rubynuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ba9def/32.png) [@rubynuby](https://www.ruby-forum.com/u/rubynuby)\
**Post date:** [November 30, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/how-to-implement-go-to-next-previous-item/121768/3 "2007-11-30T22:20:22Z")

</div>

you should know the ID of the object so can you run a query to get the  
next or  
previous post?

Just a though

WW
