#254 Pagination with Kaminari
If you need pagination in Rails 3 try out Kaminari. It is a clean, flexible solution which uses scopes to flow nicely with Active Record queries. Did I mention it also works with Mongoid?
- Download:
- source code
- mp4
- m4v
- webm
- ogv
finally we get good option for will_paginate!
A recent change in rails edge make will_paginate rails3 branch not work any more so I switched to kaminari today. It took only half an hour .. awesome plugin!
Easy pagination, thanks to your sharing
I love your screencasts, thanks a lot !
awesome!
The only problem I have with kaminari is that you cannot change the parameter used from :page. This means it isn't possible to have multiple paginations on a page. Though, this probably isn't a common problem.
I do like it much better than will_paginate though, because it allows its based on scopes, which means you don't get weird errors when trying to apply other scopes on a paginated object like you do will_paginate.
Fixed now (posting for anyone else reading this)
Yep, I second that Allen, being able to change the page parameter's name is actually a crucial capability to me that is missing. I need it again and again, so I don't think its so uncommon. But I doubt it is difficult to change Kaminari to support this.
Fixed now (posting for anyone else reading this)
Good paginate gem! I love you!
Can you create custom paginated collections?
Wow, I was just recently looking for an alternative to will_paginate that was compatible with Mongoid. Thanks again for the just-in-time railscast, Ryan!
I am bored with will_paginate. So, I decided to use kaminari.
This is so awesome. i've reluctantly been using will_paginate but knew it could be done much better with aRel. Thanks for the great cast. I'm switching today.
thank you once again!
Why this is better than will_paginate?
Good find! Thank you!
I love that it uses HTML5 tags, e.g. <nav>
Some browsers don't understand HTML5 tags yes, e.g. Firefox <= 3.6
To work around this, tell the browser which new tags are blocks:
header, footer, nav, article, section, aside { display: block; }
Agreed Allen,
That's a primary function in an application I've built. I wonder if that is something they plan on adding in the future.
you read my mind with this railscast - was looking for something like this
@ryanb Thank you for introducing Kaminari!
@Allen @Nico @Branden Just added that feature and released a new version. Can you try out 0.10.2 gem? Thank you very much @Allen for you cooperation!
@Tilo Great tip! I shall write that on my Wiki.
I'm sorry I forgot to mention how to.
<%= paginate @products, :param_name => :pagina %>
Enjoy!
i think kaminari has to much views.
i have build my own paginator, that not need so much views. it use only ul/li's with good css class for styling or hidding.
https://github.com/phatworx/rails_paginate
@20
a groupby result is not paginatable, because the result is a key value response.
u must use subselects like:
where("id IN (YOUR SELECT)")
Akira Matsuda and Ryan, thanx guys!!
@23
Sorry but i don't know what you mean. Could you please explain it or give me a example? -Thx
Hi there, did you get kaminari to work with meta_search results?
@20 Just released 0.10.3 gem that can hopefully handle group query. https://github.com/amatsuda/kaminari/commit/1de679
Could you update your gem and try again?
Thanks!
does it work with datamapper?
nice) it works on my site!
Adding kaminari to my Gemfile has stopped pickle steps being recognised by cucumber. Anyone else noticed this?
Can you use an image link instead of Previous or next
Small gotcha if anyone gets caught up on this...if you are attempting to paginate an ActiveRecord relation that has had the includes method called on it, and you include another table with a column name that is the same as the table you're paginating (like created_at), and you're trying to order on that column, kaminari can't figure it out.
You have to do something like this:
@posts = Post.order("posts.created_at DESC").page(params[:page]).per(5).includes(:comments)
#fail i used this gem and nothing return all objects exist in my database!
Since you have Railscasts for JanRain or OpenID Selector, would you like to add that to the log in system here?
I wonder why some users show up with their user name, but some as "/users/123"... is it something with the field name...?
I'm also wondering if Kaminari will work with meta_search. Will poke at it and see
The only problem I found is that params[:page] is not set to 1 for page one and defaults to nil.
Just in case anyone else was looking for solution Kaminari+Meta_Search, long story short, I added the :page under #Results, debug... into meta_search.rb
there is a reference at:
http://metautonomous.lighthouseapp.com/projects/53012/tickets/51-make-it-working-with-kaminari
change made per latest commit by author:
https://github.com/ernie/meta_search/commit/4fc9ab8f9c47b48e733ce867c5919f009a66d7c6
since then, working very well
I love your screencasts, thanks a lot !
For the AJAX version,
you add a ":remote => true" argument to the paginate() call from index.html, then add something like this to the index.js.erb:
https://gist.github.com/876500
No application.js hooks needed, with unobtrusive JS, since any link with data-remote already has a .live() hook attached to it.
Anyone got this working on Mongoid?
I only get error messages. 1 for 0 argument error when I try to pass anything on page. per method doesn't work claiming something about can't assign per to fixnum 1.
What if your data is obtained from an API (going through net), not from your own DB... then how do you use Kaminari (or will_paginate) ?
I am using Kaminari to paginate the display of messages from a database of email messages. In my Show view for an individual message I want to add Next and Previous message links that navigate to the next and previous messages in the list of messages shown on the Kaminari-paginated page. I can't figure out how, from the Show view, to access the array of records that are displayed on a particular page. The message IDs are not necessarily consecutive.
The Search results are stored in @messages but I can't access that from the Show view.
How can I access the array of 25 messages for a page using params[:page]?
**Leigh
Hey guys and Ryan, I added Mongomapper support to kaminari.. it's in the latest version :)
Great tutorial, I tried out this gem and quite suprised how easy it is. However I don't I will use it yet as will_paginate is having great meta_search integration that few of my projecst are heavily based.
Besides, will_paginate is going improvement to support rails3 simple scope style API, wait and see.
Very nice.
Wow! This rocks! I was getting tired of will_paginate, especially not knowing if at any point the rails3 branch was going to screw up on me. I definitely recommend Kaminari over will_paginate any day! And it's not any harder to implement either......
Kaminari is also SEO-friendly! Add a route like this:
And Rails will automatically rewrite your URLs to be SEO-friendly! Instead of
/products?page=32
, you'll have a nicer URL -/products/page/32
. Because of this, you can also leverage on some page-caching techniques if your index isn't updated frequently.By the way, will_paginate has now been updated for Rails 3!
https://github.com/mislav/will_paginate
Looks like they have updated the :views file. If you're wanting to add the First/Previous/Next/Last in the view so that it doesn't disappear when appropriate, just remove the lines
unless current_page.first?
unless current_page.last?
in the
_paginator.html.erb
file@Dave Kimura:
... done:
= paginator.render do
%nav.pagination
= first_page_tag #unless current_page.first?
= prev_page_tag #unless current_page.first?
- each_page do |page|
- if page.left_outer? || page.right_outer? || page.inside_window?
= page_tag page
- elsif !page.was_truncated?
= gap_tag
= next_page_tag #unless current_page.last?
= last_page_tag #unless current_page.last?
but it doesn't work :-(
instead of commenting out, try to remove the
#unless current_page.first?
#unless current_page.last?
Done,
but doesn't seem to work anyway.
My last working gems matrix is :
bson (1.4.0)
bson_ext (1.4.0)
mongo (1.4.0)
mongoid (2.1.1)
mongoid-rspec (1.4.4)
mongoid_taggable_with_context (0.7.2)
as soon as I upgrade to mongoid 2.1.2,
kaminari '0.12.4' First/Previous/Next/Last in the view don't show up anymore ...
@Dave Kimura:
you're right, I wrongly used erb commenting syntax into haml environment. That's for app/views/kaminari/_paginator.html.haml ...
but what about
= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote
= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote
rispectively in :
./app/views/kaminari/_prev_page.html.haml
and
./app/views/kaminari/_first_page.html.haml
How do you change them ?
Note to newbs, if you experience undefined method errors. Restart your server
Thanks for catching that, I've corrected it :)
what the kaminari corresponding of page_entries_info?
I use it with will_paginate but I didn't find it in kaminari.
I decided to use Kaminari in one of my projects I am updating to Rails 3.1. Just in case anyone has SEARCH functionality in their application, the original code probably looks like this:
if params[:search].nil?
@users_paginated = [].page(params[:user_page])
elsif params[:search].blank?
@users_paginated = [].page(params[:user_page])
else
@users_paginated = user_search_method # this is just a method I created for my project.
end
Your new code should probably look like this:
OR, if you wanted to return the first group of users in the database when no param is entered or if the result is blank, you could do the following:
I hope this helps someone.
doesn't work when multiple paginates on the same page , even using different :param_name .... there is only one 'current_page' checked
need to be improved ...
I'll be back to will_paginate ....
Am using kaminari ajax pagination in my project. It is working fine, But contents are displaying number of pages times in the one page. For example if number of items per pages is 7, then it is displaying 7 times the same content (7 items). What am doing is
In product_details controller
In product_details/index.html.erb
In product_details/index.js.erb
In product_details/_product_detail.html.erb
Please help me to solve this problem
I know this thread is a bit old, but I was wondering if anyone has tried to pass the page param through a link to the show page.
Say I have a paginated list of users and I click on the show link for a particular user to see the info for that user, OK. HOWEVER, when I click the "Back" link it just does the index page from the start, instead of the "page" that it was on.
So, I am not sure how to go about passing the page param back and forth between the show page and the index page.
This seems like something most everyone would want?...
So, how did you accomplish that?
This may be a newbie question but here goes...
Suppose my data is date based. (Like events on a calendar), and I want my "pages" to correspond to some units of time. For example, some number of weeks.
So that page 1 is events occurring in next 2 weeks from today... and Page 2 is events occurring in the next 2 weeks.... etc.
In this case, a page isn't a fixed number of items. It may be many or few.
In this use case, is pagination via kaminari something I should be using, or should I be hacking together something specific to my scenario? And if so, does anyone have any pointers to where to go for advice?
Thanks,
John
Heh, I answered my own question. In my case, I grouped my events by start.to_date, an then took the keys and paginated that array. This allowed me to return the first page of so many dates, and then the next page, and it doesn't matter how many calendar dates fall between them. (so I don't have pages of empty data this way for gaps in the dates)
tl;dr: I figured it out. Thanks!
i am using :window=>1 for my pagination. by this i'm able to sho only 2 pages i.e. 1,2 when user 1st time see, but i want to show here 1,2,3..... using window =>1.
How can i show 3 page when 1st time user see the page.
Can you pls elobarate how should i use this
hai, i have error from kaminari pagination. here is the problem:
undefined method `page' for...
controller
def index
@users = User.order("created_at").page(params[:page]).per(50)
end
view:
<%= paginate @users %>
generate:
rails g kaminari:config
rails g kaminari:views default
I have put gem 'kaminari' in my gemfile.
please help me. thanks you.
Thank you very much. This saved me a lot of time.
Just wanted to say, thanks for this. Been able to change the html saved my A$$. I wrote this cute pagination style and regular markup(alas will_p) wasn't gonna cut it.
Hi all. Question:
I have my root to "entries#index"
When I paginate entries with <%= paginate @entries %> visiting the root (/), the pages links goes to /entries?page=N instead to root as /?page=N.
How would I tell kaminari to do that?
Thanks!
so years later in rails 4, is Kaminari still better, cleaner, and frequently updated as will_paginate?
Kaminari seems to not have the prev_span_tag and the prev_span files.
It uses certain methods to reference the partials but the ones for disabled ones are missing. So how do we create a method to reference a custom disabled partial we make from scratch?
Hi all,
can you help me config action view from 1 2 3 4 5 to default 10 number (1 2 3 4 5 6 7 8 9 10)?
- when I click 1, 2, 3, 4, 5 still (1 2 3 4 5 6 7 8 9 10)
- when I click more than 5 ( display left 4 + current + 5 right)
Thanks all!!!