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. 调整页面显示:
<%= will_paginate @post,
:inner_window => 1,
:outer_window => 1,
:prev_label => '上一页',
:next_label => '下一页'
%>

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;
}

你可能感兴趣的:(java,SVN,css,Rails)