简体   繁体   中英

Pagination with an array of hashes in Rails?

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.

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

你将在这里使用will_paginate gem和一个简单的演示http://gerardogc2378.blogspot.mx/2011/10/paginando-objetos-con-willpaginate.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM