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?

Jackson Cunningham
  • 4,973
  • 3
  • 30
  • 80

2 Answers2

13

I think you have to assign

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

in your controller.

Krisdigital
  • 630
  • 1
  • 8
  • 16
  • i am facing above issue even after adding this i'm having the same issue please help me in it – Vishal Taj PM May 27 '17 at 02:39
  • For me it didn't work without passing the `total_count` to `paginate_array`. Check this answer [here](https://stackoverflow.com/questions/33523671/kaminari-paginate-array-argumenterror-wrong-number-of-arguments-2-for-1#answer-33524108) – Ruby Jul 06 '17 at 08:35
0
 @users = current_shop.active_users.page(params[:page]).per(10)

Do it on the ActiveRecord::Collection

j-dexx
  • 10,286
  • 3
  • 23
  • 36
  • 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_array – Jackson Cunningham Oct 02 '15 at 17:17
  • 1
    It's an active record collection, not an array. So you're better off just letting kaminari add scopes to that. – j-dexx Oct 05 '15 at 08:47
  • 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) – Jackson Cunningham Oct 06 '15 at 22:01
  • 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 repo – j-dexx Oct 07 '15 at 08:21