uma
uma

Reputation: 63

undefined method `total_pages' for #<ActiveRecord::Relation (Kaminari gem)

I don't understand why Kaminari is not working, I Have

admins_controller.rb

class AdminsController < ApplicationController
    before_action :authenticate_admin!
    

    def index
        # Admin access to the order tables
        @orders = Order.all.page(params[:page]).per(5)
        @q = Order.ransack(params[:q])
        @orders = @q.result.order(created_at: :desc)
    end

    
end

views/admins/index.html.erb

</table>
  
    <div class="paginator">
      <%= paginate @orders %>
    </div>

This above code is not at all working with kaminari and gives an error

`undefined method `total_pages' for #<ActiveRecord::Relation [#<Order id: 159......`

BUT, I also have another controller and index page with custom scope which works perfectly fine

second_admins_controller.rb

class SecondAdminsController < ApplicationController
    before_action :authenticate_admin!

    def index
        @picks = Pick.where(shop:"Sapna Xerox Near PDA College").order(created_at: :desc).page(params[:page]).per(5)
        @q = Order.ransack(params[:q])
        @orders = @q.result.order(created_at: :desc)
    end
end

views/second_admins/index.html.erb

</table>
    <div class="paginator">
      <%= paginate @picks %>
    </div>

This second_admins_controller.rb and its index.html.erb has pagination and it works perfectly WHEREAS The admins_controller.rb and its index.html.erb throws an error! any solutions? I tried looking up at other Stackoverflow solutions but non of them helped!

Upvotes: 0

Views: 320

Answers (1)

Sergey Sokolov
Sergey Sokolov

Reputation: 994

You redefine @orders variable, so it is used without .page(params[:page]).per(5)

You can try this

def index
    # Admin access to the order tables
    @q = Order.ransack(params[:q])
    @orders = @q.result.order(created_at: :desc).page(params[:page]).per(5)
end

Upvotes: 1

Related Questions