3 Quick quiz: Where does pagination logic belong?
10 We think you know the answer (if you think hard enough).
12 This plugin makes magic happen. You *will* paginate!
17 Use a paginate finder in the controller:
19 @posts = Post.paginate_by_board_id @board.id, :page => params[:page]
21 Yeah, +paginate+ works just like +find+ -- it just doesn't fetch all the records.
22 Just don't forget to tell it which page you want!
24 Render the posts in your view like you would normally do. When you need to render
25 pagination, just stick this in:
27 <%= will_paginate @posts %>
29 You're done. (Copy and paste the example fancy CSS styles from the bottom.)
31 How does it know how much items to fetch per page? It asks your model by calling
32 +Post.per_page+. You can define it like this:
34 class Post < ActiveRecord::Base
35 cattr_reader :per_page
41 class Post < ActiveRecord::Base
47 ... or don't worry about it at all. (WillPaginate defines it to be 30 if missing.)
48 You can also specify the count explicitly when calling +paginate+:
50 @posts = Post.paginate :page => params[:page], :per_page => 50
52 The +paginate+ finder wraps the original finder and returns your resultset that now has
53 some new properties. You can use the collection as you would with any ActiveRecord
54 resultset, but WillPaginate view helpers also need that object to be able to render pagination:
57 <% for post in @posts -%>
58 <li>Render `post` in some nice way.</li>
62 <p>Now let's render us some pagination!</p>
63 <%= will_paginate @posts %>
68 Ruby port by: PJ Hyett, Mislav Marohnić (Sulien)
69 Contributors: K. Adam Christensen, Chris Wanstrath, Dr. Nic Williams
70 Original announcement: http://errtheblog.com/post/929
71 Original PHP source: http://www.strangerstudios.com/sandbox/pagination/diggstyle.php
73 REPORT BUGS on Lighthouse: http://err.lighthouseapp.com/projects/466-plugins/overview
78 Copy the following css into your stylesheet for a good start:
85 padding: 2px 5px 2px 5px;
87 border: 1px solid #aaaadd;
88 text-decoration: none;
91 .pagination a:hover, .pagination a:active {
92 border: 1px solid #000099;
95 .pagination span.current {
96 padding: 2px 5px 2px 5px;
98 border: 1px solid #000099;
100 background-color: #000099;
103 .pagination span.disabled {
104 padding: 2px 5px 2px 5px;
106 border: 1px solid #eee;