Will_paginate error undefined method `total_pages'

72 Views Asked by At

I'm constantly getting the error when trying to paginate my products:

undefined method `total_pages' for #<Product::ActiveRecord_Relation:0x007fa19efed0c0>

I've tried lots of combinations but It still shows. I've also tried to require 'will_paginate/array' in an initializer and in application controller with no luck.

¿Can someone help with this please?

Products Controller:

  def from_category
    @category = Category.find(params[:cat_id])
    @products = Product.where(category_id: params[:cat_id])
    @filtered_products = @products.paginate(page: params[:page], per_page: 30)
    respond_to do |format|
      format.js
    end
  end

In erb view

<%= will_paginate @filtered_products %>

index.js.erb

$("#products").html("<%= escape_javascript(render("products")) %>");

1

There are 1 best solutions below

0
Palash Bera On

you can try like this:

# app/controllers/products_controller.rb

  def from_category
    @category = Category.find(params[:cat_id])
    @products = Product.where(category_id: params[:cat_id]).paginate(page: params[:page], per_page: 30)

    respond_to do |format|
      format.js
    end
  end

For erb file you can use like:

<%= will_paginate @products %>

Can you please try like this?