I've noticed that pagination gems like mislav-will_paginate
are quite popular. Is this because Rails does not have a built-in pagination solution or because the built-in solution is not very good?
views:
5037answers:
4Rails 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.
I'd recommend searchlogic. It has pagination built in and many other nices things.
- 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):
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:
config.gem "searchlogic"
and install it with: rake gems:install
Also checkout the online example
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 concensus seems to be that using 'classic_pagination' is not optimal.
After watching a couple of podcasts and after serveral recommendations I opted to try the will_paginate plugin and haven't looked back. It's fast, easy to use and well-maintained.
I believe that even V2 of Searchlogic recommends its use.
Seems searchlogic uses will_paginate, so you don't need to use searchlogic to get it. It looks pretty cool, though.