# Question about pagination

**URL:** <https://www.ruby-forum.com/t/question-about-pagination/56495>\
**Category:** Rails\
**Created:** [March 31, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/question-about-pagination/56495 "2006-03-31T18:02:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 31, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/question-about-pagination/56495/1 "2006-03-31T18:02:17Z")

</div>

Hi!

I’ve got a list of project on the main page and if one of them is  
selected i render a partial using ajax with data of selected project.

How to create links to next/previous project using pagination?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 31, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/question-about-pagination/56495/2 "2006-03-31T18:22:50Z")

</div>

use the pagination\_links.

```
 @customer_pages, @customers = paginate(:customers, options_hash)

```

then in your view you have access to @customer\_pages which is a  
paginator, and therefore can do things like this:

\<% paginator = @customer\_pages %\>

\<%= unless paginator.current == paginator.first then link\_to  
(“first”, :page =\> paginator.first)  
else ‘first’ end %\>

\<%= if paginator.current.previous then link\_to(“prev”, :page =\>  
paginator.current.previous)  
else ‘prev’ end %\>

\<%= if paginator.current.next then link\_to(“next”, :page =\>  
paginator.current.next)  
else ‘next’ end %\>

\<%= unless paginator.current == paginator.last then link\_to  
(“last”, :page =\> paginator.last)  
else ‘last’ end %\>

| \<%= pagination\_links @customer\_pages, {:window\_size =\> 5} %\>

It gets even more fun if you’re passing a hash of values for  
conditions (like search filtering, or pop-up-value filtering… then  
you need to build an options hash, too… and you end up like this:

\<% # options hash for pagination links  
opt\_hash = { :search\_box =\> @search\_box }  
opt\_hash[:is\_something] = @is\_something unless(@is\_something.empty?)  
# etc…  
%\>

\<% params\_hash = opt\_hash # grab the params hash before the :page key  
of it has been set - for the pagination links down below %\>

\<%= unless paginator.current == paginator.first then  
opt\_hash[:page] = paginator.first  
link\_to(“first”, opt\_hash)  
else ‘first’ end %\>

\<%= if paginator.current.previous then  
opt\_hash[:page] = paginator.current.previous  
link\_to(“prev”, opt\_hash)  
else ‘prev’ end %\>

\<%= if paginator.current.next then  
opt\_hash[:page] = paginator.current.next  
link\_to(“next”, opt\_hash)  
else ‘next’ end %\>

\<%= unless paginator.current == paginator.last then  
opt\_hash[:page] = paginator.last  
link\_to(“last”, opt\_hash)  
else ‘last’ end %\>

| \<%= pagination\_links @customer\_pages, {:window\_size =\> 5, :params  
=\> params\_hash} %\>

Julian.

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 1, 2006, 9:40am UTC](https://www.ruby-forum.com/t/question-about-pagination/56495/3 "2006-04-01T09:40:46Z")

</div>

Thanks for explaining this!

But i still have a problem.  
I have a list of projects. If one is selected i pass its id to ‘show’  
action.  
It looks like this:

@project = Project.find(params[:id])  
@project\_pages, @projects = paginate :projects, :per\_page =\> 1  
render :partial =\> “project”, :locals =\> {:project =\> @project,  
:project\_pages =\> @project\_pages}

How to somehow connect my @project with @project\_pages paginator, so it  
will know which project is currently displayed and which is  
next/previous? If i try to display any link i.e. like this:

\<%= link\_to(“previous project”, :page =\>  
@project\_pages.current.previous) if @project\_pages.current.previous %\>

it is not displayed.

Am i passing the paginator wrongly to my partial?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 2, 2006, 11:47am UTC](https://www.ruby-forum.com/t/question-about-pagination/56495/4 "2006-04-02T11:47:51Z")

</div>

Well it doesn’t work correctly…

My list of projects in the menu is sorted by date. So when the user  
chooses one and then selects next projects using ‘next project’ link  
he/she can expect to get next project from the list.

But because i use position column i get next project from the table, not  
from the list.

I checked in api and in paginate method there’s no option to set current  
page, and in Paginator.new there’s no option to set conditions. And even  
if could set conditions and current page, i still know only selected  
project position in the table, not in the sorted list.

So how to do it?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 2, 2006, 11:09am UTC](https://www.ruby-forum.com/t/question-about-pagination/56495/5 "2006-04-02T11:09:52Z")

</div>

I’ve managed to do this, but maybe someone knows an easier way.

I added acts\_as\_list to my Project model, which allowed me to get model  
instance position and set :page property of paginator correctly.

BTW. Does someone know how to write mysql procedure to update all values  
in ‘position’ column automatically?
