Searchkick: undefined method `paginate` error when using api-pagination gem

Created on 6 Jan 2018  路  5Comments  路  Source: ankane/searchkick

I'm in the process of setting up my api and am using searchkick along with api-pagination/will_paginate. Here is how my index action is setup:

def index
  if params[:q]
    @cuisines = Cuisine.search params[:q], fields: [:name]
  else
    @cuisines = Cuisine.all
  end

  paginate json: @cuisines, each_serializer: Api::CuisineSerializer
end

When I hit this url:

http://api.test-site.dev/admin/cuisines?q="american"

I get the this exception thrown:

#<NoMethodError: undefined method `paginate' for #<Searchkick::Results:0x007fc586002618>>

While searching the web for a solution I was brought back here on git but to no avail:

paginate json: Cuisine.search("american").results

yeilds

#<ArgumentError: Must specify fields to search>

Specifying the fields brings me back to an exception similar to my original:

#<NoMethodError: undefined method `paginate' for #<Array:0x007fc580f22220>>

Any idea on how to fix this? I'm using: rails (5.1.4) and the latest versions of all said gems.

Most helpful comment

Thanks for following up with the solution. I'm sure others will find it useful.

All 5 comments

@TKB21 Try following instructions in the ArgumentError message.

@ankane Tried the following:

paginate json: Cuisine.search("american", fields: [:name]).results

but ran into the error I was displaying in the original post:

#<NoMethodError: undefined method `paginate' for #<Array:0x007fc580f22220>>

Not sure if that's what you meant. Doing some further research I found the only workaround was to require will_paginate/array in my controller(s). With this in mind do you think this is problem is more geared towards will_paginate? This issue is non-existent using kaminari just as a side note.

@TKB21 Sorry, missed that. If requiring will_paginate/array fixes it, I don't think there's an issue with either project. It's just something you'll need to do.

Cool. Thanks for the clarification.

Thanks for following up with the solution. I'm sure others will find it useful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NeMO84 picture NeMO84  路  3Comments

netwire88 picture netwire88  路  3Comments

josephfarina picture josephfarina  路  3Comments

gerrywastaken picture gerrywastaken  路  3Comments

JensDebergh picture JensDebergh  路  4Comments