---
title: "[Rails 常用插件简介]will_paginate"
author: "夜鸣猪"
date: "2008-05-10T13:34:20Z"
tags: []
slug: "rails-will-paginate"
draft: false
lastmod: "2008-05-10T13:34:20Z"
id: 191722
url: "https://hlee.iteye.com/blog/191722"
---

Rails2.0中原pagination将被移出作为插件classic\_pagination，will\_paginate是个非常不错的替代方案

一：安装

[\[Copy to clipboard\]](http://hinder.iteye.com/admin/blogs/#%23%23) [\[ - \]](http://hinder.iteye.com/admin/blogs/#%23%23)

CODE:

[view plain](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[copy to clipboard](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[print](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[?](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)

1. ruby script/plugin install svn://errtheblog.com/svn/plugins/will\_paginate

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

 

二：使用

Controller

[\[Copy to clipboard\]](http://hinder.iteye.com/admin/blogs/#%23%23) [\[ - \]](http://hinder.iteye.com/admin/blogs/#%23%23)

CODE:

[view plain](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[copy to clipboard](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[print](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[?](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)

1. **@posts** = Post.paginate\_by\_board\_id **@board**.id, :page =&gt; params\[:page]||1

```
 @posts = Post.paginate_by_board_id @board.id, :page => params[:page]||1
```

 

如我们以前的find一样

在View中显示分页

[\[Copy to clipboard\]](http://hinder.iteye.com/admin/blogs/#%23%23) [\[ - \]](http://hinder.iteye.com/admin/blogs/#%23%23)

CODE:

[view plain](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[copy to clipboard](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[print](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[?](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)

1. &lt;%= will\_paginate **@posts** %&gt;

```
<%= will_paginate @posts %>
```

 

那么如何设定每页显示记录条数？paginate将会调用mode的per\_page,譬如上面的Post将会调用Post.per\_page.或者直接在paginate中加上per\_page  
1:) 在model中

[\[Copy to clipboard\]](http://hinder.iteye.com/admin/blogs/#%23%23) [\[ - \]](http://hinder.iteye.com/admin/blogs/#%23%23)

CODE:

[view plain](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[copy to clipboard](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[print](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[?](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)

1. class Post &lt; ActiveRecord::Base
2. cattr\_reader :per\_page
3. **@@per\_page** = 50
4. end

```
    class Post < ActiveRecord::Base
      cattr_reader :per_page
      @@per_page = 50
    end
```

 

[\[Copy to clipboard\]](http://hinder.iteye.com/admin/blogs/#%23%23) [\[ - \]](http://hinder.iteye.com/admin/blogs/#%23%23)

CODE:

[view plain](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[copy to clipboard](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[print](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[?](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)

1. class Post &lt; ActiveRecord::Base
2. class&lt;&lt;self
3. def per\_page
4. 50
5. end
6. end
7. end

```
    class Post < ActiveRecord::Base
      class<<self
          def per_page
            50
          end
      end
    end
```

 

2:) 在controller中

[\[Copy to clipboard\]](http://hinder.iteye.com/admin/blogs/#%23%23) [\[ - \]](http://hinder.iteye.com/admin/blogs/#%23%23)

CODE:

[view plain](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[copy to clipboard](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[print](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[?](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)

1. **@posts** = Post.paginate :page =&gt; params\[:page]||1, :per\_page =&gt; 50

```
@posts = Post.paginate :page => params[:page]||1, :per_page => 50
```

 

如果没有设定，将会使用默认值30

will\_paginate 对原有的find做了包装，并且新增几个特性，如下：

[\[Copy to clipboard\]](http://hinder.iteye.com/admin/blogs/#%23%23) [\[ - \]](http://hinder.iteye.com/admin/blogs/#%23%23)

CODE:

[view plain](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[copy to clipboard](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[print](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)[?](http://www.ruby-lang.org.cn/forums/thread-985-1-1.html)

1. page           REQUIRED, but defaults to 1 if false or nil
2. per\_page       (default is read from the model, which is 30 if not overriden)
3. total entries  not needed unless you want to count the records yourself somehow
4. count          hash of options that are used only for the call to count

```
page           REQUIRED, but defaults to 1 if false or nil
per_page       (default is read from the model, which is 30 if not overriden)
total entries  not needed unless you want to count the records yourself somehow
count          hash of options that are used only for the call to count
```

 

注：page 属性是必须的