Kaminari: undefined method `total_pages' for Array
Asked Answered
G

2

6

Using Rails & Kaminari gem, I am getting the below error when I render my view:

undefined method `total_pages' for #Array:0x007faa486583e0

controller:

def index
    @user = current_shop.users.new
    @users = current_shop.active_users   ### This returns an array
    Kaminari.paginate_array(@users).page(params[:page]).per(10)
 end

view:

     <tbody>
       <%= paginate @users %>

        <% @users.each do |user| %>
          <%= render 'user_table_row', :user=> user %>
       <% end %>

     </tbody>

What am I doing wrong?

Gillian answered 2/10, 2015 at 16:8 Comment(0)
W
14

I think you have to assign

@users = Kaminari.paginate_array(@users).page(params[:page]).per(10)

in your controller.

Worser answered 29/10, 2015 at 12:4 Comment(2)
i am facing above issue even after adding this i'm having the same issue please help me in itRann
For me it didn't work without passing the total_count to paginate_array. Check this answer hereDisquieting
F
0
 @users = current_shop.active_users.page(params[:page]).per(10)

Do it on the ActiveRecord::Collection

Finis answered 2/10, 2015 at 16:10 Comment(4)
current_shop.active_users is an array. (active_users is a shop method to using .where(user_status: active) ). That's why I was using Kaminari.paginate_arrayGillian
It's an active record collection, not an array. So you're better off just letting kaminari add scopes to that.Finis
current_shop.users is a collection. I have a method: "def active_users users.where(status: "active").reverse end" which returns an array. I want to paginate that array (current_shop.active_users)Gillian
I imagine it'd be quicker to do the ordering in the db query. But I see nothing wrong in what you've done with having an array so I'd suggest raising an issue in the kaminari repoFinis

© 2022 - 2024 — McMap. All rights reserved.