Rails2中使用will_paginate插件实现分页

介质下载:
http://github.com/mislav/will_paginate/tree/master
参考文档API:
http://gitrdoc.com/mislav/will_paginate/tree/master/

1.下载will_paginate插件并解压,拷贝至vendor/plugins下;
2.will_paginate插件主要用在controller和view两个层级上,在controller生成一个paginator对象,通过Object.paginate实现,相当于一个find查找,但是对查找的结果集进行了分页的处理,带有相关的options,可以实现对页面的控制。
3.在View层,通过<%= will_paginate @products, :prev_label => '上一页', :next_label => '下一页' %>生成相应的分页显示的链接。

示例,修改部分代码如下:
在控制器层 products_controller.rb
class ProductsController < ApplicationController
  def index
    #@products = Product.all
    @products = Product.paginate :page => params[:page] || 1, :per_page => 1
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @products }
    end
  end
  end
 
  end
…
End
在View层 index.html.erb
<h1>Listing products</h1>
<table>
  <tr>
  	<th>Title</th>
    <th>Description</th>
    <th>Image url</th>
    <th>Price</th>
  </tr>
<% @products.each do |product| %>
  <tr>
  	<td><%=h product.title %></td>
    <td><%=h product.description %></td>
    <td><%=h product.image_url %></td>
    <td><%=h product.price %></td>
	
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>
<hr>
<%= will_paginate @products, :prev_label => '上一页', :next_label => '下一页' %>
<hr>
<br/>
<%= link_to 'New product', new_product_path %>

最后,重启服务,访问http://127.0.0.1:3000/products即可。
其他参考资料:
http://www.iteye.com/topic/154713
http://www.blogjava.net/fl1429/archive/2009/03/09/258601.html
http://www.desimcadam.com/archives/8
修改will_paginate支持ajax方式:
http://www.iteye.com/topic/188607
http://9esuluciano.iteye.com/blog/137090

你可能感兴趣的:(html,xml,Ajax,.net,Ruby)