Paginating an Array in Ruby with will_paginate

kaminari, ruby, ruby-on-rails, ruby-on-rails-3, will-paginate

Solution

Sure, you can use `WillPaginate::Collection` to construct arbitrary pagination across collections. Assuming an array called `values`:

@values = WillPaginate::Collection.create(current_page, per_page, values.length) do |pager|
  pager.replace values
end

You can then treat `@values` just like any other WillPaginate collection, including passing it to the `will_paginate` view helper.

Problem

I have an array @level1 which looks like this : ``` [[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4], ............] ``` I want to apply pagination on this array such each page displays only a few rows at once. I tried this: ``` @temp1 = @level1.paginate(:page => params[:page]) ``` But it throws the following error: ``` undefined method `paginate' for [[3.0, 4, 2], [2.0, 48, 3], [2.1, 56, 4]]:Array ``` How can I perform pagination on this using will_paginate?

Original source