kaminari undefined method 'page'

user2184718 picture user2184718 · Jan 30, 2014 · Viewed 7.6k times · Source

I am trying to add Kaminari to my Rails app. I have included the gem and this is what my controller looks like:

def index
    if params[:year]
      if params[:year].size > 0
        @songs = Song.where("year like ?", params[:year]).page(params[:page])
      elsif params[:artist].size > 0
        @songs = Song.where("artist_name like ?", params[:artist]).page(params[:page])
      elsif params[:song].size > 0
        @songs = Song.where("title like ?", params[:song]).page(params[:page])
      end
    else
      @songs = Song.first(10).page(params[:page])
    end
  end

and then adding

<%= paginate @songs %>

in my view, the error I am getting is:

undefined method `page' for #<Array:0x007fab0455b4a8>

Not sure why this is coming up as I followed the docs step for step.

Answer

AbM picture AbM · Jan 30, 2014

Kaminari uses paginate_array to paginate an array. 2 solutions:

First, you can use limit(10) instead of first(10):

@songs = Song.limit(10).page(params[:page])

Second, use paginate_array

@songs = Kaminari.paginate_array(Song.first(10)).page(params[:page])