Search code examples
ruby-on-railsrubypaginationsunspotwill-paginate

undefined method `paginate' issue


I have the following code in my controller:

def index
  @search = Product.search do |query|
    query.fulltext params[:sSearch]
    query.with(:store_id, @store.id)
  end
  products = @search.results.paginate(page).per_page(per_page)
end

private
  def page
     params[:iDisplayStart].to_i/per_page + 1
  end

  def per_page
     params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
  end

but I get the error:

undefined method `paginate' for #<Sunspot::Search::PaginatedCollection:0x007f86243ac440>

Solution

  • Try to use within the search block:

    @search = Product.search do |query|
        query.paginate page: page, per_page: per_page
    end
    

    You do not need to use the query variable in the block. You can invoke methods directly within the block.

    @search = Product.search do
        fulltext params[:sSearch]
        with(:store_id, @store.id)
        paginate page: page, per_page: per_page
    end