Paginate collection bug

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

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.

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

On 5 Dez. 2007, 13:23, Shuaib85 [email protected] 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

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 %>