#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 60 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. weskycn Jun 29, 2007 at 01:36

when rails go to 2.0?
anyone give me a messege?
Rails2.0什么时候发布啊(还是说中国话舒服^_^)


4. Vladislav Jun 29, 2007 at 01:44

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


5. 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.


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

Great overview, thanks.


7. riki Jun 29, 2007 at 11:02

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


8. 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.


9. 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.


10. 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.


11. 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.


12. 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.


13. 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


14. 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.


15. 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


16. 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!


17. 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!


18. neokain Jun 30, 2007 at 12:54

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


19. 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 :)


20. 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?


21. 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.


22. 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


23. 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?


24. 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?


25. 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.


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

That does mean a default RESTful design of applications ??


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

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


28. 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>.


29. 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!!!


30. LOL Jul 26, 2007 at 14:51

THAT was gay ;)


31. 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?


32. 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.


33. 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.


34. bbb Sep 05, 2007 at 18:56

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


35. sss Sep 14, 2007 at 03:38

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


36. 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


37. metalman Oct 08, 2007 at 13:07

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


38. 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


39. 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?


40. 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.


41. 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?


42. Erinc Nov 06, 2007 at 17:33

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


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

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


44. 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?


45. 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


46. 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


47. 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...


48. anouar Mar 05, 2008 at 23:37

Thank you for this video.


49. 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.


50. 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 %>


51. Adrian Apr 16, 2008 at 09:50

Is it here or the repository for will_paginate is down ?


52. Shikha May 01, 2008 at 21:10

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


53. Hans May 05, 2008 at 09:10

the repository seems to be down

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

:(


54. 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'


55. kino May 23, 2008 at 01:56

Whence comes transcendental logic, the solution of which involves the relation between the objects in space and time and our sense perceptions?


56. 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.


57. 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


58. 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


59. 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


60. no Sep 07, 2008 at 00:02

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

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