# Paginate collection bug

**URL:** <https://www.ruby-forum.com/t/paginate-collection-bug/122186>\
**Category:** Rails\
**Created:** [December 5, 2007, 11:26am UTC](https://www.ruby-forum.com/t/paginate-collection-bug/122186 "2007-12-05T11:26:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [December 5, 2007, 11:26am UTC](https://www.ruby-forum.com/t/paginate-collection-bug/122186/1 "2007-12-05T11:26:49Z")

</div>

Hello,

I was looking to do pagination for collection and I found this bunch  
of code.

def paginate\_collection(collection, options = {})  
default\_options = {:per\_page =\> 5, :page =\> 1}  
options = default\_options.merge(options)

```
pages = Paginator.new self, collection.size, options[:per_page],

```

options[:page]  
first = pages.current.offset  
last = [first + options[:per\_page], collection.size].min  
slice = collection[first…last]  
return [pages, slice]  
end

@country = WorkInCountry.find(params[:id])  
@emp = @country.employees  
@country\_pages, @countries = paginate\_collection @emp, :per\_page =\> 5

in the view, it will display all the output and the number of pages,  
once you click on next page, it will display the same thing as the  
first page and so on.

any idea on how to fix this error.

Regards

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [December 5, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/paginate-collection-bug/122186/2 "2007-12-05T13:05:30Z")

</div>

On 5 Dec 2007, at 10:26, Shuaib85 wrote:

> @country\_pages, @countries = paginate\_collection @emp, :per\_page =\> 5
> 
> in the view, it will display all the output and the number of pages,  
> once you click on next page, it will display the same thing as the  
> first page and so on.

You need to tell it what page to display (ie, pass in :page =\> 5 to  
show the fifth page)

Fred.

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [December 5, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/paginate-collection-bug/122186/3 "2007-12-05T13:23:43Z")

</div>

Well, I did try passing the page number and yet the problem is the  
same.  
I found this snap of code in different place and the same problem  
appears

def paginate\_collection(coll, options = {})  
# Grab params and fix em up  
per = options[:per\_page].to\_i  
page = (params[:page] || 1).to\_i  
offset = (page - 1) \* per

```
 # Create a Paginator based on the collection data
 pages = Paginator.new self, coll.size, per, page

 # Grab the sub-set of the collection
 paged = coll[offset..(offset + per - 1)]

 return [pages, paged]

```

end

@country = WorkInCountry.find(params[:id])  
@emp = @country.employees  
@country\_pages, @countries = paginate\_collection @emp, :per\_page =\> 5

is there another function built in rails that does the job?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [January 15, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/paginate-collection-bug/122186/4 "2008-01-15T13:23:51Z")

</div>

On 5 Dez. 2007, 13:23, Shuaib85 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, I did try passing the page number and yet the problem is the  
> same.

You can pass the page number to the method on the controller side like  
this:

@foo\_pages, @foos = paginate\_collection @bar.foos, :per\_page =\>  
3, :page =\> params[:page]

A little bit more typing but it works.

Greetings,  
Chris

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [January 5, 2008, 10:44am UTC](https://www.ruby-forum.com/t/paginate-collection-bug/122186/5 "2008-01-05T10:44:14Z")

</div>

Hi,

copy the below statement in paginate\_collection method  
…  
@page = params[:country\_pages]  
…  
copy the below code in your view and run it. it will definitely work…  
…  
\<%= "Page: " + pagination\_links(@country\_pages, :params =\> { :action  
=\> params[“action”] || “index” }) + “

* * *
” if @pages.page\_count \>  
1%\>

\<%= link\_to ‘Previous page’, { :page =\>  
@country\_pages.current.previous } if @country\_pages.current.previous  
%\>  
\<%= link\_to ‘Next page’, { :page =\> @country\_pages.current.next } if  
@country\_pages.current.next %\>  
…
