ITeye博客

Paginate a collection or an array

koska · 2007-02-05 13:31 · Ruby On Rails · Markdown

def paginate_collection(collection, options = {})
    default_options = {:per_page => 10, :page => 1}
    options = default_options.merge options
    pages = Paginator.new self, collection.size, options[:per_page], options[:page]
    first = pages.current.offset
    last = [first + options[:per_page], collection.size].min
    slice = collection[first...last]
    return [pages, slice]
end

The above snippet can be invoked using
@pages, @users = paginate_collection User.find_custom_query, :page => @params[:page]

评论(3)

  1. #3 楼 · thegiive · 2007-02-06 08:02

    我以前有花點時間寫這個東西 Maybe helpful

    http://lightyror.thegiive.net/2006/11/pagination-collection.html

  2. #2 楼 · koska · 2007-02-05 17:17

    def list_for_one_category @category = Category.find(params) @article_pages, @articles = paginate_collection @category.articles, :per_page => 10 end

  3. #1 楼 · wiwolf · 2007-02-05 16:54

    如果能有一个完整的例子就好了

评论功能维护中,当前仅展示历史评论。