2015-05-18 43 views
0

我想在我的視圖中顯示所有標準。但它應該每頁顯示10條記錄。所以我用will_pagination來處理這種情況。當我點擊第一頁時,它顯示了S.No的前10個標準記錄。但是當我點擊第二頁時,第11條記錄的S.No顯示「1」。但S.No應該是11的紀錄。它不工作。會有什麼問題?S.No在分頁鏈接頁面「2」上單擊時沒有刷新

控制器:

@standards = Standard.select("standards.standard_id, standards.standard_name") 
        .where("standards.org_id = ?", org_id) 
@standards = @standards.paginate(:per_page => 10, :page => params[:page]) 

查看:

<% if (@standards != nil && @standards.length > 0) then%> 
     <% @standards.each.with_index(1) do |standard,index| %> 
      <tr> 
      <td> <%= index %></td> 
      <td> <%= standard.standard_name %></td> 
      </tr> 
     <% end %> 
     <div class="text-right"> 
      <% if @standards != nil then%> 
      <%= will_paginate @standards, :class => 'pagination-xs', renderer: BootstrapPagination::Rails %> 
      <%end%> 
     </div> 

回答

1

用參數值來獲得序列號:

<% count = ((params[:page] || 1).to_i - 1) * 10 %> 

<% if (@standards != nil && @standards.length > 0) then%> 
    <% @standards.each.with_index do |standard,index| %> 
    <tr> 
     <td> <%= count + index %></td> 
     <td> <%= standard.standard_name %></td> 
    </tr> 
    <% end %> 
    <div class="text-right"> 
    <% if @standards != nil then%> 
     <%= will_paginate @standards, :class => 'pagination-xs', renderer: BootstrapPagination::Rails %> 
    <%end%> 
    </div> 

UPDATE

在你的控制器代碼:

@standards.paginate(:per_page => 10, :page => params[:page]) 

你是說,你需要,每頁10筆,你已經設置頁碼PARAM爲params[:page]

一開始,我們沒有得到任何params因爲它因此如果沒有收到參數值,我們必須將其作爲1.

params[:page] || 1 

在這種情況下,count = (1-1)*10 = 0

考慮你在第二頁上,讓你的URL是這樣的:

localhost:3000/standards?page=2 

現在你有params[:page] = 2

而且count = (2 - 1)*10 = 10

所以10將被添加到序列號的第二頁中的每條記錄。

+0

嘿男人..感謝很多......這解決了我的問題。但我不明白,你想要說的是什麼。你能解釋我嗎? – Anitha

+0

@AnitaShalu:爲了更好的理解,我已經更新了答案。如果解決了問題,請接受答案。 – webster

+0

但我仍然面臨一個新問題。前10個記錄以S.No 1到10開始。當我點擊第二頁時,它開始11 ..但是當我點擊第3頁時,它以21開頭。爲什麼這樣? – Anitha