Paginating an Array in Ruby with will_paginate Paginating an Array in Ruby with will_paginate ruby ruby

Paginating an Array in Ruby with will_paginate


See https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb

Just require 'will_paginate/array' before you try it and it will work just fine. If it were me, I'd put that in say config/initalizers/will_paginate_extensions.rb so it's available right away and from everywhere.


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 valuesend

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


Given a collection array in a Rails project

will_paginate ~>3.0.5collection.paginate(page: params[:page], per_page: 10)

returns the pagination for the array values.

Do not forget require 'will_paginate/array' on top of the controller.