Thursday, February 17, 2011

Will Paginate - Array Pagination

Wanna paginate an Array using will paginate.. Here is how i did..

Add this as method,

Array.class_eval do
def paginate(page=1, per_page=15)
pagination_array = WillPaginate::Collection.new(page, per_page, self.size)
start_index = pagination_array.offset
end_index = start_index + (per_page - 1)
array_to_concat = self[start_index..end_index]
array_to_concat.nil? ? [] : pagination_array.concat(array_to_concat)
end
end

And with the array @results do @results.paginate

reference: http://www.devchix.com/2007/07/23/will_paginate-array/

Regards,
Srikanth
Learn to Program with Ruby | Ruby Pocket Reference

No comments: