views:

787

answers:

1

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

I'm trying to use both of these tools to enable users to search contacts and return a paginated list (or the entire paginated list if they don't enter any search criteria). However I'm unsure of the proper way to chain them together, and what I'm 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 gives me the error Undefined method 'all' for WillPaginate. Removing the all gives me an error because the view is looking for a path that has the word "contact" 20 times (e.g. contact_contact_contact..._path), presumably because the default "per page" is 20.

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

+5  A: 

I was getting confused by this as well. 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 count (will_paginate automatically adds that in).

As soon as you call .paginate it triggers the query. So when you even though you think you are setting @search to a Searchlogic object, you aren't. You are setting it to a WillPaginate array which has no method .all that you can call.

Greg DeVore
Excellent tip. Thanks.
Chalkers