Using searchlogic with will_paginate

EDIT Looks like I figured it out - I had to call paginate after the call all

from Searchlogic.

I am trying to use both of these tools so that users can search for contacts and return a paginated list (or the entire paginated list if they don't enter any search criteria). However, I am not sure about the correct way to combine them and what I am trying is giving me errors.

Here is my controller:

class ContactsController < ApplicationController
  def index
    @search = Contact.search(params[:search]).paginate(:page => params[:page])
    @contacts, @contacts_count = @search.all, @search.count
  end
end

      

This is giving me an error Undefined method 'all' for WillPaginate

. Removing all gives me an error because the view is looking for a path that has the word "contact" 20 times (for example contact_contact_contact..._path

), presumably because the default "on page" is 20.

What am I doing wrong? I would like this page to have search, ordering and pagination.

+2


source to share


1 answer


I got confused by this too. You want to do the following:

class ContactsController < ApplicationController
  def index
    @search = Contact.search(params[:search])
    @contacts = @search.paginate(:page => params[:page])
  end
end

      



In your view, just call @ contacts.total_entries to get the total counter (will_paginate automatically adds this).

As soon as you call .paginate it triggers the request. So when you even think you are setting @search on a Searchlogic object, you are not. You are setting it to the WillPaginate array, which has no .all method you can call.

+6


source







All Articles