#51
Jun 29, 2007

will_paginate

In edge rails (soon to be Rails 2.0), the built-in pagination has been moved into a plugin: classic_pagination. I recommend jumping over to the will_paginate plugin as shown in this episode.
Tags: plugins
Download (19 MB, 6:48)
alternative download for iPod & Apple TV (10.6 MB, 6:48)

Resources

# models/product.rb
def self.search(search, page)
  paginate :per_page => 5, :page => page,
           :conditions => ['name like ?', "%#{search}%"],
           :order => 'name'
end

# products_controller.rb
def index
  @products = Product.search(params[:search], params[:page])
end
<!-- products/index.rhtml -->
<%= will_paginate @products %>

RSS Feed for Episode Comments 67 comments

1. Clemens Jun 29, 2007 at 00:38

Definitely a good choice for pagination .... Great tutorial as usual!


2. chineseGuy Jun 29, 2007 at 00:47

neat!~


3. Vladislav Jun 29, 2007 at 01:44

+1, great cast!
ф пятерке!


4. InMan Jun 29, 2007 at 06:40

Using Rails Edge almoust month. It really rocks :).
Thats good railscast too. Just thought that i should search something about pagination tomorrow.


5. Geoff Buesing Jun 29, 2007 at 10:11

Great overview, thanks.


6. riki Jun 29, 2007 at 11:02

Aaah very cool. How could we combine this with a List sort function?


7. dave Jun 29, 2007 at 13:18

I've been using paginate_find and it works pretty well. Have any opinions/experience with that plugin?

The paginate in the model ability from will_paginate looks really cool.


8. Ryan Bates Jun 29, 2007 at 13:54

@riki, not exactly sure what you're referring to. You can pass an ":order" option just like the "find" method so you can do any sorting you wish.

@dave, I haven't used paginating_find but it looks pretty good.


9. lisa Jun 29, 2007 at 15:10

Does will_paginate do AJAX pagination? Paginating_find does not (currently) and if will_paginate can do this I may migrate to it.


10. Ryan Bates Jun 29, 2007 at 15:59

@lisa, nope, AFAIK will_paginate doesn't offer anything special in regards to AJAX. You can still use the back end part of the plugin to manage the pagination, however, you may need to rewrite the view portion to use link_to_remote instead of normal links.


11. dave Jun 29, 2007 at 19:10

"kinda" rails related, but a short screencast of how you do your shortcut magic in textmate might be cool.


12. Chris Kampmeier Jun 29, 2007 at 20:49

Lisa - it's pretty easy to write a helper to do your AJAX pagination. There's a good example in the (awesome) exception_logger plugin. It's designed for classic pagination, but something like this would work for will_paginate, too. Check it out: http://svn.techno-weenie.net/projects/plugins/exception_logger/lib/logged_exceptions_helper.rb


13. lisa Jun 30, 2007 at 03:36

I could also write a helper for AJAX paginating links for paginating_find...

Wouldn't mind seeing how these two plugins stack up with each other.


14. riki Jun 30, 2007 at 09:28

I found an Ajax approach here that lets you "sort, search and paginate"

http://dev.nozav.org/ajaxtable/?sort=name


15. AndyKram Jun 30, 2007 at 10:29

Is it wrong that I was giddy like a small child on Christmas day when I saw the new Railscast in my iTunes Podcasts directory? Thanks for all your awesome work on these podcasts!


16. Mislav Jun 30, 2007 at 12:52

Hi, I'm the main maintainer for the will_paginate plugin. Ryan, thanks a lot for this screencast! For the rest of you, you can get familiar with the plugin by generating the RDoc documentation; simply type "rake rdoc" while in the plugin directory.

If you want to report bugs or suggest features for this plugin, go to http://err.lighthouseapp.com/projects/466-plugins. Thanks!


17. neokain Jun 30, 2007 at 12:54

That good ep. I wish you create episode about testing on rails. Thaink you


18. Nicolás Sanguinetti Jun 30, 2007 at 15:09

@lisa, with UJS it'd extremely simple to layer ajax pagination on top of will_paginate. Check out ujs4rails.com :)


19. apso Jun 30, 2007 at 21:23

thanks for pointing the way to will_paginate. however, i was trying to implement it to paginate "entries" that works with the "acts_as_taggable" plugin. i placed this in the entry model.

  def self.taggedwith(tag, page)
    paginate :conditions => ["tags.name=?",tag],
      :joins => "INNER JOIN taggings ON entries.id = taggings.taggable_id INNER JOIN tags ON taggings.tag_id = tags.id",
      :order=>"entries.created_at DESC",
      :per_page => 10,
      :page => page
  end

while i do get the correct entries, the tag_list are always incorrect. could you suggest any workaround?


20. Markus Arike Jul 01, 2007 at 11:47

Great screencast as usual. I have learned so much from your podcasts that my rails abilities have improved considerably.

Unfortunately, the will_paginate plugin won't work with my version of rails (1.2.3). I'm not able to start mongrel, as I get "finder.rb:11:in `included': undefined method `alias_method_chain'". Shame as I really want to use it for an artists website that I'm redesigning.

Thanks again, Ryan. You are the man.


21. Mislav Jul 01, 2007 at 17:14

@Markus,

I've pointed out the link to submit bug reports to in my previous comment. And the error you're seeing *can't* be right because Rails 1.2 uses alias_method_chain internally all over the place! If you keep seeing the error, submit a proper report with more information. Thanks


22. Dave Giunta Jul 02, 2007 at 09:40

I'm having trouble installing the will_paginate plugin using <code>script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination</code>.

I keep getting a timeout error saying subversion can't connect to host 'errtheblog.com'. Anyone have any ideas? Is anyone else having this problem?


23. Mahmoud MHIRI Jul 20, 2007 at 10:58

Thanks Ryan, this is the only thing I needed when I decided to move to the edge Rails!

Just a curiosity question: What the scaffolding in rails 2.0 will generate to paginate pages?


24. Ryan Bates Jul 20, 2007 at 11:14

@Mahmoud, the scaffolding in edge Rails uses what is currently called scaffold_resource. This doesn't do any pagination.


25. Mahmoud MHIRI Jul 20, 2007 at 12:30

That does mean a default RESTful design of applications ??


26. Ryan Bates Jul 20, 2007 at 16:25

@Mahmoud, yep. The default scaffolding will be RESTful in Rails 2.0.


27. Zargony Jul 22, 2007 at 05:29

Great screencast, thanks a lot.

I'm using the will_paginate plugin myself for a while now and I really like it. However I've come across some problems while trying to paginate special queries (e.g. custom finder class-methods on your model). I've put up a summary in <a href="http://zargony.com/2007/07/21/paginating-special-queries/>this post in my blog</a>.


28. yly Jul 26, 2007 at 04:21

If I was gay I'd definitly would want to be your boyfriend!!!! Your casts are so seeeexy! For real!!!


29. LOL Jul 26, 2007 at 14:51

THAT was gay ;)


30. bilson Aug 18, 2007 at 18:34

I'm getting no response at all when attempting to install from this address...
svn://errtheblog.com/svn/plugins/will_paginate

Could there be a problem in Windows? I don't seem to have this problem with any other plugins?


31. Ryan Bates Aug 20, 2007 at 10:20

@bilson, it's working for me. Is it still not working for you? Maybe Windows doesn't like the "svn" protocol. Not sure what the solution is.


32. bilson Aug 20, 2007 at 13:06

My mistake. It is working now. I thought I could download via svn from the command line, but I have to do so directly through the Tortoise interface instead.

Great screencast. Great plugin.


33. bbb Sep 05, 2007 at 18:56

你说中国话谁看的懂啊,sb啊
rails2。0早着呢
.. ... .
Hello,everybody!


34. sss Sep 14, 2007 at 03:38

to bbb:
其他人是看不懂中国话,不过还有你这个sb可以看得懂啊。


35. The Batman Oct 05, 2007 at 12:22

Mislav,

I modified view_helpers.rb to add clean and simple ajax support.

Look here http://batmanrails.blogspot.com/2007/10/ajax-support-for-willpaginate.html

-Batman


36. metalman Oct 08, 2007 at 13:07

你說中国話,我說中國話...


37. Daniel Passos Oct 10, 2007 at 07:21

@Ryan Bates,

Install will_paginate with svn:// not work to me too. After I install SVN command line this work


38. Charles Oct 18, 2007 at 15:44

Hey,

I really need to get this thing to work wiht ajax, i need a link to remote to update a specific id and I can't get any info on how to do that...plus I am not very good with rails...can someone help out with this?


39. andy Nov 02, 2007 at 14:01

Ajaxify will_paginate

instruction left by @The Batman are outdated (already!). Err, or whomever maintains will_paginate, really needs to add Ajax support. Without this plugin is not "game-day" ready.

I hacked up a version that works for me but it's not publishable.

I would look for another paginator that supports Ajax before using this one.


40. charlie Nov 06, 2007 at 08:46

Hello, I have will_paginate working with lowpro. It does ajax pagination just fine. Except that I have two partials in the same view, and I cannot figure out how to make each work separately. The first page renders fine, because each partial draws from a separate instance variable in the controller. But when you click on another page for each partial, each partial sends the same exact message to the controller. Each partial says "give me page two for this view", rather than "give me page two for this partial". Any hints?


41. Erinc Nov 06, 2007 at 17:33

this videocast was extremely helpful to me, thank you for your hard work.


42. Héctor Nov 12, 2007 at 06:33

Charles, try:
http://railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes


43. Bala Paranj Jan 25, 2008 at 14:46

If I have 100 pages how do I display pages 1 through 5 then .... and the last 5, 95 through 100?


44. Ned Feb 29, 2008 at 02:47

Would anyone know how to use this in conjunction with form parameters? I have a form, the parameters of which go into my find / paginate function. This works fine for the first results page, but when a link is followed to subsequent pages, the search parameters are lost. Is there a way to pass a parameters hash via the will_paginate helper method in the view???
The problem is explained in more detail here:
http://www.railsforum.com/viewtopic.php?id=14604


45. Ryan Bates Feb 29, 2008 at 10:51

@Ned, will_paginate should automatically pass the params. Make sure to use GET request in your search form along with the helper methods which end in "_tag" (such as "text_field_tag" and "check_box_tag"). See this episode for details:
http://railscasts.com/episodes/37


46. Ned Mar 03, 2008 at 06:23

Thanks for the reply Ryan - I'll check this out when I get the chance - I think I might be using different form helper methods, which might explain why the parameters aren't passed...


47. anouar Mar 05, 2008 at 23:37

Thank you for this video.


48. samani ismael Apr 03, 2008 at 09:29

I am having a problem displaying my data in the view side, I am using partials.

<%= will_paginate @my_object %>

and I only see the pagination links on the browser but I can't see the data, although when I try to loop through @my_object without using will_paginate I was able to get the data, I don't know what I am doing wrong I followed the same steps from this screencast.


49. Alexander Boldt Apr 15, 2008 at 07:16

@samani ismael

Hi, the method will_paginate doesn't display the data, only handles the paging.

<% for object in @my_object do %>
 <%= object.id %>
<% end %>
<%= will_paginate @my_object %>


50. Adrian Apr 16, 2008 at 09:50

Is it here or the repository for will_paginate is down ?


51. Shikha May 01, 2008 at 21:10

How can we pass get parameters when user navigates to enother pages using will_paginate?


52. Hans May 05, 2008 at 09:10

the repository seems to be down

http://require.errtheblog.com/ -> moving servers. be back soon.

:(


53. Joel May 06, 2008 at 00:12

I'm assuming that there's no option for ASC/DESC ordering is there?

ie: :sort_order => 'ASC'


54. Housni Jun 03, 2008 at 18:14

Hey Ryan, great screencasts. I've learnt 99% of my Rails from them :)

I have a question though...looking at my logs, it looks like the SQL generated uses 'like' everytime since the :conditions is used. Doesn't this take a very small performance hit?

What would be the best way to exclude the 'like' search except when a search term is passed? Use a condition to check if the 'search' parameter exists and then append it to the paginate code?

Thanks.
Keep up the great work.


55. Shuaib Zahda Jun 22, 2008 at 08:19

Hi, Thanks for the very informative episode

Now we are facing problem with the new will_paginate in rails 2. Can you make a screencast for us on how to use it because we keep getting errors

Thanks


56. bijou Aug 07, 2008 at 03:39

hey ryan! i'm trying this out today. it looks great in your tutorial! so hopefully it'll work for me.

noticed that the installer changed too, after the plugin just refused to get itself installed. please be advised :)

http://errtheblog.com/posts/56-im-paginating-again


57. Conrad Taylor Aug 24, 2008 at 20:58

Hi Ryan, I was wondering, what do you use to generate the 'Previous' and 'Next' links that we see above?

Thanks in advance,

-Conrad


58. no Sep 07, 2008 at 00:02

This screencast was very useful. Thank you for doing this!


59. tcpipmen Sep 13, 2008 at 14:48

please let me know how can i use will_paginate with alphabets. i.e i have a dictionary with words from A to Z. so i would like to paginate A, B, C, .., Z as pages and clicking A for instance will show all words start with alphabet A. kindly let me know. many thanks in advance.


60. Guillaume Oct 13, 2008 at 14:20

Thanks Ryan for this screencast.

I have created a blog entry on how to ajaxify will_paginate thanks to lowpro:
http://hasnosorrow.wordpress.com/2008/10/13/ajaxify-wil_paginate-with-lowpro/

Hope that helps,
Guillaume


61. Louis Oct 20, 2008 at 11:34

Very, very helpful. Thanks for your time and effort with this. You are a great instructor.

-Louis


62. Roy Oct 20, 2008 at 15:54

Is there any way to get on the view what is the amount of objects I want to display per page? Thanks!


63. ryan Oct 28, 2008 at 07:48

Hey ryan, is there a good way to modify the will_paginate helper if you want to page or action cache the pagination.

rails envy had a tutorial on adding to the routes.rb the necessary route and what the new pagination link should look like, but is there a way to do that automacially in the view?


64. Bradd Jan 26, 2009 at 16:54

Hey, great blog, lots of useful stuff. I'm a bit of an RoR noob and this site has helped my out a lot. Is a there a simple way to build in a flash message for when the search results come up with nothing?

--Thanks a bunch


65. tschundeee Feb 05, 2009 at 23:53

Hi Ryan!

I really love your screencasts and I will definitely donate some bucks to you :-)
One thing you could update is the source to the plugin. They are now on github and the maintainers strongly advise not to use the deprecated svn-repo :)

URL:
http://github.com/mislav/will_paginate/tree/master

KR tschundeee


66. mrudula Apr 11, 2009 at 06:42

i am very much new to rails.This gave me a very good idea.Thank u. please post good tutorials from scratch so that it gives a fair idea for us.


67. Kruby Apr 18, 2009 at 06:08

Love your screencasts.
This is my first contribution to the RoR community, so I hope it is a help.
I searched a long time for a solution, until I discovered it was right there in will_paginate.

paginate_by_parent_id nil, ...

made it possible to add an extra edge to my finds, so that the search is only for posts that do not have a parent_id.
Could not make this work with :conditions alone.

Here is the code from my model post.rb:

def self.search_list(search, page)
paginate_by_parent_id nil,
  :per_page => 5, :page => page,
  :conditions => ['title like ? or body like ?',"%#{search}%","%#{search}%"],
  :order => 'created_at DESC'
end


68. pulkit Apr 24, 2009 at 01:10

Hi guys,

For Ajax Pagination visit my blogpost.

http://pulkitonrails.wordpress.com/2009/04/24/ajax-pagination/


69. Erkan Jun 13, 2009 at 18:14

Ryan how can stop this
THISSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS


70. Sergio Jul 28, 2009 at 02:34

Hi, the svn server in no working, can you bring an alternative?


71. Puneet Aug 20, 2009 at 05:32

Really a Nice Screencast. Just a quick doubt,
When using will_paginate is it necessary that we should keep our search parameter in session?
for example
view
<input type="text" name="q">
method
@product = Product.paginate(:all, :conditions=>["name LIKE ?", "%#{params[:q]}%"], :page=>params[:page], :per_page=>10)

it is giving me those results which are not matched with the criteria, when I am scrolling through pages.. any help?


72. vintage wedding dress Sep 12, 2009 at 09:10

Good post,thank you for sharing.


73. Weber Grill Accessories Sep 16, 2009 at 06:58

nice tips


74. Narayana Oct 29, 2009 at 04:19

very great tutorial.It is really useful for fresher.It would be great if you add Ajax functionality with pagination.Looking forward for your reply.
I want an example with will_paginate,Ajax with out using JQuery.


75. baby high chair Nov 27, 2009 at 09:22

will_paginate is great!!!!


76. Chaika Dec 22, 2009 at 02:11

great!!!(:


77. peter Jan 05, 2010 at 03:04

ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

not working


78. dimianstudio Jan 06, 2010 at 11:36

use ruby script/plugin install http://github.com/mislav/will_paginate.git


79. Protocol Testing Training Bangalore Feb 02, 2010 at 01:50

Thank you for the great web site - a true resource, and one many people clearly enjoy thanks for sharing the info, keep up the good work going....


80. Giridhar SK Feb 18, 2010 at 06:10

Hey Ryan. Great work! Can i ask u a question though , kinda stupid actually. But i was wondering how to combine will_paginate and searchlogic? if u could help me out that'll be great..


81. NJ Refi Mar 13, 2010 at 18:38

Nice one ryan,

It's great that you would help out everyone like this.


82. louie vuitton Apr 10, 2010 at 05:18

Thank you


83. Matt Jun 01, 2010 at 19:07

This is working great for me! My only problem is that I'm using the :page_links => false. It's working correctly by taking out the numbered links and only displaying Prev and Next for the links.
But those two links are on separate lines. Prev is on one and Next is on the next line in the browser. I tried using :style => 'display: inline' so that they would be on the same line, but it's not working.
I'm sure I'm messing this up somewhere. Anyone else having the same issue?


84. Never Give Up Dude Jun 17, 2010 at 04:58

i like this post too


85. Blogger Indonesia Dukung Internet Aman, Sehat & Manfaat Jul 01, 2010 at 23:34

These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post, writing is simply great, thank you for the post


86. Day Trans Travel Jakarta Bandung Jul 01, 2010 at 23:35

Thanks for your consise and relevant insight.


87. Day Trans Travel Jakarta Bandung Jul 01, 2010 at 23:36

Thank you very much for this information.
Good post thanks for sharing.
I like this site ;)


88. facebook luna maya Jul 03, 2010 at 04:51

good consept, very useful information nice post keep it


89. Nike Air Jordan Shoes Jul 05, 2010 at 20:45

Nike Air Jordan Shoes


90. grup seyran Jul 24, 2010 at 03:16

thanks for post. nice.


91. Indonesia Furniture Handicraft Wholesale Marketplace Aug 02, 2010 at 00:30

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!


92. Travel Jakarta Bandung Aug 02, 2010 at 00:39

There is obviously a lot to know about this. I think you made some good points in Features also.


93. mesa doctor Aug 03, 2010 at 03:54

I have been following you blog for quite some time now…..I just wanted to say how excited and happy I am for you. I can’t wait to see what you come up with.


96. pll fm transmitter Aug 09, 2010 at 19:51

Does will_paginate do AJAX pagination? Paginating_find does not (currently) and if will_paginate can do this I may migrate to it.


97. Air Jordan Force 4 Aug 23, 2010 at 22:48

Good job! very good cord.This is all very new to me and this article really opened my eyes. thank you


98. louis vuitton shoes Aug 26, 2010 at 21:18

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


99. snow boots Aug 31, 2010 at 01:27

It's designed for classic pagination, but something like this would work for will_paginate, too.


100. levis belts Sep 01, 2010 at 21:07

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you’ve been doing this for a while now, how long have you been blogging for?

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source