适用于 Rails 及其他 Ruby 应用的分页库
will_paginate 是一个分页库,可与 Ruby on Rails、Sinatra、Hanami::View 和 Sequel 集成。
gem 'will_paginate', '~> 4.0'更多信息请参阅 wiki 上的安装说明。
ℹ️ will_paginate 目前处于维护模式,不会再添加新功能。查看替代方案
## perform a paginated query:
@posts = Post.paginate(page: params[:page])
# or, use an explicit "per page" limit:
Post.paginate(page: params[:page], per_page: 30)
## render page links in the view:
<%= will_paginate @posts %>就这样!完成了。你只需添加一些 CSS 样式来让分页链接更美观。
你可以自定义默认的 "per_page" 值:
# for the Post model
class Post
self.per_page = 10
end
# set per_page globally
WillPaginate.per_page = 10Active Record 3 中的新特性:
# paginate in Active Record now returns a Relation
Post.where(published: true).paginate(page: params[:page]).order(id: :desc)
# the new, shorter page() method
Post.page(params[:page]).order(created_at: :desc)更多文档请参阅 wiki。在 GitHub 上报告 bug。
祝分页愉快。
暂无开放 Issues,或尚未同步最近议题。