1

I have a simple array of hashes retrieved from Redis in my Ruby on Rails application. These are a list of items that I'd like to paginate in a view. They are not ActiveRecord based or based on any database ORM wrapper. They're simply retrieved like this:

@items = $redis.lrange("items-#{params[:id]}", 0, -1)

At this point I want to do something as simple as list 10 items per page and allow the user to paginate through them.

Is there a plugin/tool available that works on straight arrays? It seems most stuff is focused on ActiveRecord.

randombits
  • 47,058
  • 76
  • 251
  • 433
  • For will_paginate, you can see answer here: http://stackoverflow.com/questions/11551522/how-do-i-paginate-a-hash-of-arrays-using-the-kaminari-or-will-paginate-gem/20112337#20112337 – Hoang Le Nov 21 '13 at 04:18
  • For will_paginate, you can see answer here: http://stackoverflow.com/questions/11551522/how-do-i-paginate-a-hash-of-arrays-using-the-kaminari-or-will-paginate-gem/20112337#20112337 – Hoang Le Nov 21 '13 at 04:19

2 Answers2

6

If you use the Kaminari gem, you can add pagination to an array.

items = $redis.lrange("items-#{params[:id]}", 0, -1)
@items = Kaminari.paginate_array(items).page(params[:page]).per(10)

Then in your view it's as simple as

@items.each do |item|
   ...
end
paginate @items
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
-1

You will use will_paginate gem and a simple demo here http://gerardogc2378.blogspot.mx/2011/10/paginando-objetos-con-willpaginate.html

ggc
  • 1