簡體   English   中英

使用will_paginate gem獲取記錄在rails中的當前頁面

[英]Getting the records count at current page in rails with will_paginate gem

我需要獲取當前頁面的記錄數量。 我使用will_paginate gem進行分頁。 在嘗試獲取collection.count時,我獲得了總記錄數?

@collection = User.paginate(page: params[:page], per_page: 10).all

當我嘗試獲取收集計數時,它返回總條目。 如何獲取當前表的記錄數

嘗試:

@collection.total_entries

如果您需要整個查詢中的總條目:

@collection.total_entries

如果您需要當前頁面中的輸入計數:

@collection.size

同時你不需要在分頁中調用最后的.all范圍。 你試一試:

@collection = User.paginate(page: params[:page], per_page: 10)

如果你想要當前頁面上的當前項目數,你應該做@collection.size 如果你想要你應該做的項目總數@collection.count

您可以嘗試將page設置為最后一頁以查看差異。

也用

@collection = User.paginate(page: params[:page], per_page: 10)

因為paginate只返回相應的set,配置了pageper_page參數。

我還想找到當前頁面的記錄數。 但@ surveys.size沒有返回當前頁面中的記錄數,而是返回當前頁面已填滿的記錄數。 Rails 5.2.2 ruby​​ 2.5.1p57(2018-03-29 Revision 63029)[x86_64-linux]

@collection.length 

是提供所需結果的那個。

@collection.entries

正在為您提供當前頁面上的條目。 例如,如果您分頁25個條目,它將顯示Array類中的25個條目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM