will_paginage

will_paginate 安装

介绍: will_paginate是rails最好的分页插件, 功能强大,使用简单。

Gem版本安装与使用:

1
2
gem sources -a http://gems.github.com
sudo gem install mislav-will_paginate

在windowns平台下,不需要sudo。

在config/environment.rb的最后,加上require

1
2
3
4
5
Rails::Initializer.run do |config|
  ...
end

require "will_paginate"

Plugin版本安装


script/plugin install git://github.com/mislav/will_paginate.git

will_paginate 使用

在controller中,查询时,使用paginate


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

在view中,用will_page @post显示分页的链接

1
2
3
4
5
6
7
<ol>
    <% for post in @posts -%>
      <li><%= post.title %></li>
    <% end -%>
</ol>

<%= will_paginate @posts %>, 共有 <%= @posts.total_entries %> 篇文章

Wiki首页 | 查看所有 | 编辑 | 输出到博客 | 历史版本