2011-07-20 51 views

回答

3

的第一頁是相當容易:

link_to 'First Page', :action => :whatever, :page => 1 

最後一頁有點棘手,在你的模型類添加:

class << self 
    # number of records per page, from the will_paginate docs 
    def per_page 
    20 
    end 

    # takes a hash of finder conditions and returns a page number 
    # returns 1 if nothing was found, as not to break pagination by passing page=0 
    def last_page_number(conditions=nil) 
    total = count :all, :conditions => conditions 
    [((total - 1)/per_page) + 1, 1].max 
    end 
end 

現在你可以這樣做:

link_to 'Last page', :action => :whatever, :page => Model.last_page_number 
1

假設您正在分頁@users,然後將這些鏈接放在您的視圖中

<%= link_to 'First Page', :action => 'index', :page => 1 %> 
<%= will_paginate @users %> 
<%= link_to 'Last Page', :action => 'index', :page => @users.page_count %> 
+0

如果page_count是一個返回頁數的方法,那將是一個很好的解決方案。不幸的是,will_paginate沒有在 –

+0

內建立的方法,但我在rails 2.8中使用這種方法,它對我的​​工作很好。 – Mahesh