nicolaswecandoit nicolaswecandoit - 3 months ago 17
Ruby Question

Kaminari and Elasticsearch NoMethodError Rails

I have one error with Kaminari and Elasticsearch on rails app. I am unable to find the problem. I think my mistake is in my controller. By the way I am using active-admin for administrate my app.


undefined method `page' for "France":String


Elasticsearch.rb

case
when defined?(::Kaminari)
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari
when defined?(::WillPaginate)
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::WillPaginate
end


Controller:

def result
if params[:q].nil?
@campings = []
else
@campings = Camping.__elasticsearch__.search params[:q].page(params[:page]).per(14)
end
end


Thanks for your help :)

Answer

The problem is you're calling the page method on params[:q] which is a string. You should enclose the call to search with brackets so that the page method will be called on the result of search and not on params[:q].

@campings = Camping.__elasticsearch__.search(params[:q]).page(params[:page]).per(14)

Hope this helps!