views:

5037

answers:

4

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?

+4  A: 

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.

Here's a good article on how to use Rails pagination

workmad3
+3  A: 

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

reto
How does this compare to the numerous other pagination solutions? Why choose this over the others?
propstop
Well, the previous thing I used was will_paginate, which used to be the default paginator. The beauty of searchlogic is a) that everything is integrated (you don't have to get paginator work together filtering/sorting, this is done automatically/magically). And b) it's pretty actively developed atm and it looks quite mature.c) the developer produces very nice and intuitive libraries, I would also recommend his auth library 'authlogic'. In the documentation you'll find some remarks why authlogic is 'better' than the other auth plugins :). But just try them out in one of your data views..
reto
Ah what a great timing: http://github.com/binarylogic/searchlogic/tree/v2
reto
And his blog post: http://www.binarylogic.com/2009/06/09/searchlogic-v2-beta-released/I still stick with my opinion that searchlogicv1 is a very nice library, and so far it hasn't let me down :).
reto
Searchlogic is not recommended to be used with Rails3 though.
jpartogi
what makes you say that? I guess it isn't ready yet, but are there other reasons against a rails3/searchlogic combo?
reto
+6  A: 

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.

digitalpardoe
A: 

Seems searchlogic uses will_paginate, so you don't need to use searchlogic to get it. It looks pretty cool, though.

http://rdoc.info/github/binarylogic/searchlogic/master/file/README.rdoc#Pagination_(leverage_will_paginate)

stretchwithme