Kaminari pagination is only returning the first 25 results not all of them

时间:2018-09-18 19:52:25

标签: ruby-on-rails kaminari

I am trying to cache requests that come in for Products. I am using the Kaminari gem for pagination but for some reason I cannot see all the products on my view page. It seems to only take the top 25 and list those.

def index

cache_key = Product::CACHE_KEY_PREFIX + params.map{|k,v| "[#{k}-#{v}]"}.join("-")

@products = Rails.cache.fetch(cache_key, expires_in: 30.minutes) do
  search_params = params.permit(:product_type,:format).to_h.symbolize_keys
  if search_params[:product_type]
    products = Product.by_product_type(params[:product_type])
  elsif params[:filters].present?
    filters = params[:filters].try(:symbolize_keys)
    products = Product.where(filters)
  else
    products = Product.all
  end
  byebug #at this point products count is 43
  products = products.page(params[:page])
  byebug #count is now 25
  @products = Kaminari.paginate_array(products.to_a).page(params[:page])
  @products #count is 25
end

respond_to do |format|
  format.html
  format.json
end

end

2 个答案:

答案 0 :(得分:0)

您需要更改此设置。

@products = Kaminari.paginate_array(products.to_a).page(params[:page])

对此:

@products = Kaminari.paginate_array(products.to_a).page(params[:page]).per(products.count)

如果未指定尺寸,则每个尺寸的默认值为25。

答案 1 :(得分:0)

来自文档。

您可以通过使用Kaminari.configure方法覆盖这些值来配置以下全局默认值

config.default_per_page = 100      # 25 by default

有一个方便的生成器,可将默认配置文件生成到config / initializers目录中。运行以下生成器命令,然后编辑生成的文件。

rails g kaminari:config