Ruby-on-rails Rails 有内置的分页解决方案吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/967183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Rails have a built-in pagination solution?
提问by propstop
I've noticed that pagination gems like mislav-will_paginateare quite popular. Is this because Rails does not have a built-in pagination solution or because the built-in solution is not very good?
我注意到分页宝石mislav-will_paginate非常受欢迎。这是因为Rails 没有内置的分页方案还是因为内置的方案不是很好?
回答by digitalpardoe
In Rails 2.0 the pagination ability of ActionController was removed and turned into a plugin for backwards compatibility called 'classic_pagination'. However, from my searches for a pagination solution for myself the consensus seems to be that using 'classic_pagination' is not optimal.
在 Rails 2.0 中,ActionController 的分页功能被移除并变成了一个名为“classic_pagination”的向后兼容插件。然而,从我为自己寻找分页解决方案来看,共识似乎是使用 'classic_pagination' 不是最佳的。
After watching a couple of podcasts and after several recommendations I opted to try the will_paginateplugin and haven't looked back. It's fast, easy to use and well-maintained.
在看了几个播客和几个推荐之后,我选择尝试will_paginate插件并且没有回头。它快速、易于使用且维护良好。
I believe that even V2 of Searchlogic recommends its use.
我相信即使是 Searchlogic 的 V2 也推荐使用它。
回答by Evgenii
If you are using Rails 3 Kaminari plugin will be very handy for pagination. GithubRailscasts
如果你使用 Rails 3 Kaminari 插件将非常方便分页。 Github Railscasts
回答by workmad3
Rails has built-in pagination, but it's a simple module and not suitable for all needs. Unless you have specific pagination needs in mind though, it should suit most purposes.
Rails 有内置的分页功能,但它是一个简单的模块,并不适合所有需求。除非您有特定的分页需求,否则它应该适合大多数目的。
回答by reto
I'd recommend searchlogic. It has pagination built in and many other nices things.
我会推荐searchlogic。它内置了分页和许多其他不错的东西。
- Easy filtering
- Pagination
- Sorting
- 轻松过滤
- 分页
- 排序
And.. for all of that nice helpers.
还有..对于所有那些好帮手。
Code says more than thousand words (dont get confused by the HAML example, you can use normal erb templates if you prefer them, the code/structure is the same):
代码说了一千多字(不要被 HAML 示例弄糊涂了,如果您愿意,可以使用普通的 erb 模板,代码/结构是相同的):
Controller:
控制器:
def index
@search = User.new_search(params[:search])
@users, @users_count = @search.all, @search.count
end
Pagination stuff in the view:
视图中的分页内容:
== Per page: #{per_page_select}
== Page: #{page_select}
Sort as/by in view:
在视图中排序为/按:
- unless @users_count.zero?
%table
%tr
%th= order_by_link :account => :name
%th= order_by_link :first_name
%th= order_by_link :last_name
%th= order_by_link :email
- @users.each do |user|
%tr
%td= user.account? ? user.account.name : "-"
%td= user.first_name
%td= user.last_name
%td= user.email
Easy, simple and quick filters:
简单、简单和快速的过滤器:
- form_for @search do |f|
- f.fields_for @search.conditions do |users|
= users.text_field :first_name_contains
= users.date_select :created_after
- users.fields_for users.object.orders do |orders|
= orders.select :total_gt, (1..100)
= f.submit "Search"
And everything works together, so changing a page and then the sorting, and adding a filter works without losing any of the other settings :).
一切都可以协同工作,因此更改页面然后进行排序和添加过滤器都可以正常工作,而不会丢失任何其他设置:)。
All you need is in your environment.rb:
您所需要的只是您的 environment.rb:
config.gem "searchlogic"
and install it with: rake gems:install
并安装它: rake gems:install
Also checkout the online example
还可以查看在线示例

