rails分页小结

最近在用 ruby on rails 实际一个项目。觉得分页很有意思。希望和大家分享,一起进步,还要像大家请教!
在controller 里的 application.rb

def paginate_collection(collection, options={})
default_options={:per_page => 5, :page => 1}
options=default_options.merge options
pages=Paginator.new self, collection.size, options[:per_page], options[:page]
first=pages.current.offset
last=[first + options[:per_page], collection.size].min
slice=collection[first...last]
return [pages, slice]
end


在具体的需要分页的controller 中

def index
@forum=Forum.find(params[:id])
@topics=@forum.get_forum_topic_object
@pages, @topics = paginate_collection(@topics, :page => params[:page]) end


在view里的写法:

<div class="breadcrumb" style="float:right;">
Displaying <%= @pages.current.first_item %>
- <%= @pages.current.last_item %>
of <%= @pages.item_count %>
<%= link_to(h('<< '),:page => @pages.first_page) %>
<%= link_to(h('< '),:page => @pages.current.previous) if @pages.current.previous %>
<%= pagination_links(@pages, :window_size => 4) %>
<%= link_to(h(' >'),:page => @pages.current.next) if @pages.current.next %>
<%= link_to(h(' >>'),:page => @pages.last_page) %>
 
</div>


显示效果:
displaying << 1 2 3 4 > >>

在view里的写法2:


<div class="breadcrumb" style="float:right;">
Displaying <%= @pages.current.first_item %>
- <%= @pages.current.last_item %>
of <%= @pages.item_count %>
<%= link_to(h('<< '),:page => @pages.first_page) %>
<%= link_to(h('< '),:page => @pages.current.previous) if @pages.current.previous %>

<% pagination_links_each(@pages, :window_size => 4) do |n| %>
<%@n=n%>
<a href='?page=<%=@n%>'>
<%=@n%>
</a>
<% end %>
<%= link_to(h(' >'),:page => @pages.current.next) if @pages.current.next %>
<%= link_to(h(' >>'),:page => @pages.last_page) %>
 
</div>


显示效果如下:
如果是第一页是显示
display << 2 3 > >>
如果是第2页则显示
display << < 1 3 > >>

是为什么啊 显示不当前页码?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值