应用gem 安装 will_paginate

rails2.0之后的版本不再支持rails1.2的paginate分页而改为will_paginate插件。在rails中应用gem 安装will_paginate的方法(windows系统):
gem install mislav-will_paginate --source http://gems.github.com
安装成功会出现以下信息:
Successfully installed mislav-will_paginate-2.3.11
1 gem installed
Installing ri documentation for mislav-will_paginate-2.3.11...
Installing RDoc documentation for mislav-will_paginate-2.3.11...

在will_paginate的wiki上还介绍了几种安装方法,详情请看http://github.com/mislav/will_paginate/wikis

新建railsProject Web应用:rails_test
在config/environment.rb添加 require 'will_paginate'
(注:必须在Rails::Initializer
#...
end
之后加入
require 'will_paginate'不然会出现:undefined method `paginate' for #<Class:0x35dbef4>错误。)

实例:
controller:

def index
@users = User.paginate :page => params[:page]||1, :per_page => 2
end
注:1是用户以http://localhost:3000/user显示的第1页的数据;2是每页显示的记录数.

view/index:

<%= will_paginate @users, :prev_label => 'pre', :next_label => 'next' %>
<hr/>
  <table border='1'>
  <% @users.each do |user| %>
  <tr>
  <td><%=h user.user_name %></td>
    <td><%=h user.age %></td>
    <td><%=h user.sex %></td>
    <td><%=h user.email %></td>
  </tr>
<% end %>
</table>

经上述方法就能实现简单的分页功能。



你可能感兴趣的:(windows,Web,Rails,rubygems)