关于分页可以参考这篇文章:
http://ruby-lang.org.cn/forums/viewthread.php?tid=206&extra=page%3D1&page=2但如果使用ajax则分页的实现需要改动一下,如下所示。
共<b><%=@article_pages.page_count%></b>页:
<%=if @article_pages.current!=@article_pages.first_page
link_to_remote("首页",:update => "articleList",
:url => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.first_page})
else
"首页"
end%>
<%=if @article_pages.current.previous
link_to_remote("上一页",:update => "articleList",
:url => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.current.previous})
else
"上一页"
end -%>
<%=pagination_links_each(@article_pages, :window_size => 5) do | page |
link_to_remote("[#{page}]", :update => "articleList",
:url => {:action => 'list',:subcategory_id => @subcategory.id,:page => page})
end%>
<%=if @article_pages.current.next
link_to_remote("下一页",:update => "articleList",
:url => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.current.next})
else
"下一页"
end -%>
<%=if @article_pages.current!=@article_pages.last_page
link_to_remote("末页",:update => "articleList",
:url => {:action => 'list',:subcategory_id => @subcategory.id,:page => @article_pages.last_page})
else
"末页"
end%>
然后再优化一下,把这些代码提取成一个公共函数,放在application_helper里,以便其他页面也能共享。代码如下:
def ajax_pagination_links(pages,update,url)
links = []
links << "共<b>#{pages.page_count}</b>页"
return links[0] if pages.page_count==1
links << ": "
if pages.current!=pages.first_page
url[:page] = pages.first_page
links << link_to_remote("首页", :update => update, :url => url)
else
links << "首页"
end
if pages.current.previous
url[:page] = pages.current.previous
links << link_to_remote("上一页", :update => update, :url => url)
else
links << "上一页"
end
links << pagination_links_each(pages, :window_size => 5) do |page|
url[:page] = page
link_to_remote("[#{page}]", :update => update, :url => url)
end
if pages.current.next
url[:page] = pages.current.next
links << link_to_remote("下一页",:update => update, :url => url)
else
links << "下一页"
end
if pages.current!=pages.last_page
url[:page] = pages.last_page
links << link_to_remote("末页",:update => update, :url => url)
else
links << "末页"
end
links.join(" ")
end
以后在页面里只需要加上这样一句即可
<%=ajax_pagination_links(@article_pages, "articleList", {:action => 'list',:subcategory_id => @subcategory.id})%>