will_paginate

will_paginate为Rails提供了非常方便的分页浏览功能。我们将通过一个小例子来展示:

1. 创建工程:

$rails test_will_paginate
$cd test_will_paginate

2. 安装插件:

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

3. 生成post模型,并添加测试数据:

$script/generate scaffold post title:string body:text
#db/migrate/001_create_posts.rb
(1..50).each do |num|
  Post.create(:title => "title#{num}", :body => "body#{num}")
end

4. 生成数据表:

$rake db:migrate

5. 定义模型默认一页显示的条目数:

#app/models/post.rb
def self.per_page
  10
end

6. 修改controller的index方法,使其支持分页:

#app/controllers/posts_controller.rb
def index
  @posts = Post.paginate :page => params[:page], :order => 'updated_at DESC'
  ......
end

7. 调整页面显示:

#app/views/posts/index.html.erb
<%= will_paginate @posts %>

Total entries: <%= @posts.total_entries %>, total pages: <%= @posts.page_count %>, current page: <%= @posts.current_page %>

8. 最后加上will_paginate推荐的css:

#public/stylesheets/scaffold.css
.pagination {
  padding: 3px;
  margin: 3px;
}
.pagination a {
  padding: 2px 5px 2px 5px;
  margin: 2px;
  border: 1px solid #aaaadd;
  text-decoration: none;
  color: #000099;
}
.pagination a:hover, .pagination a:active {
  border: 1px solid #000099;
  color: #000;
}
.pagination span.current {
  padding: 2px 5px 2px 5px;
  margin: 2px;
  border: 1px solid #000099;
  font-weight: bold;
  background-color: #000099;
  color: #FFF;
}
.pagination span.disabled {
  padding: 2px 5px 2px 5px;
  margin: 2px;
  border: 1px solid #eee;
  color: #ddd;
}

Posted in  rails plugins


blog comments powered by Disqus
Fork me on GitHub