2012-09-09 33 views
3

我有一個表和分頁到20個每頁:will_paginate增量表ID

<% @num = 0%> 
    <table> 
    <tr> 
     <th>id</th> 
     <th>title</th> 
    </tr> 

    <% for authors in @authors%> 
    <tr> 
     <td><%= @num += 1 %></td> 
     <td><%= authors.title %></td> 
    </tr> 
    <% end %> 

</table> 
<%= will_paginate @authors%> 

我想提出的ID行要繼續。當我點擊第1頁時,我應該看到1,2 3,4等id。當我點擊第二頁我應該看到21,22,23等,但它只是重新啓動,以1,2 3

回答

5

而是初始化@num爲0,將其設置爲

@authors.offset 

當你做

@authors = Author.paginate(...) 

你得到的是一個WillPaginate::Collection。這是Array的子​​類,具有諸如頁面總數,每頁的項目數量等方法。特別是,offset給出了當前頁面的偏移量。

+0

謝謝,我會嘗試。我正在使用rails 2.3 – fujisan

+0

它完美無瑕。 – fujisan

+0

此代碼如何工作?你能否詳細說明一下? – fujisan