ruby on rails 3 用will_paginate来实现分页

ruby on rails 3 用will_paginate来实现分页

编辑gemfile
gem "will_paginate", "~> 3.0.pre2"
然后
bundle install
参考
https://github.com/mislav/will_paginate/wiki/Installation
样式修改 http://thewebfellas.com/blog/2010/8/22/revisited-roll-your-own-pagination-links-with-will_paginate-and-rails-3
http://wordpress.huabaner.net/?p=190

使用
[ccn lang="ruby" tab_size="4" theme="blackboard" width="800" ]
def index
@dl_types = DlType.paginate(:page=>params[:page]||1,:per_page=>5)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @dl_types }
end
end
[/ccn]
view的写法
[ccn lang="ruby" tab_size="4" theme="blackboard" width="800" ]
<%=h will_paginate @dl_types %>
<%= page_entries_info @dl_types %>

<%=h will_paginate(@dl_types, :previous_label => '上一页', :next_label => '下一页')%>
<%= page_entries_info @dl_types, :entry_name => '分类', :plural_name => '分类' %>

<%=h will_paginate @dl_types, :previous_label => '上一页', :next_label => '下一页'%>
共<%= @dl_types.total_entries %> 条记录 <%= @dl_types.total_pages %>页 当前<%= @dl_types.current_page %>
<%= page_entries_info @dl_types, :entry_name => '分类', :plural_name => '分类' %>
[/ccn]
修改样式
写一个pagination_list_link_renderer.rb放到lib下
修改application.rb
加上
[ccn lang="ruby" tab_size="4" theme="blackboard" width="800" ]
config.autoload_paths += %W(#{config.root}/lib)
[/ccn]
修改environment.rb加上
[ccn lang="ruby" tab_size="4" theme="blackboard" width="800" ]
WillPaginate::ViewHelpers.pagination_options[:renderer] = 'PaginationListLinkRenderer'
[/ccn]