Search code examples
ruby-on-railsindexingsunspotwill-paginate

Rails: Sunspot and Will_Paginate Issue


I've hit a snag with my college project regarding the use of the Sunspot search gem and will_paginate. I've been using sunspot in my project index controller and it was working fine but when I added pagination to the same index it created a problem. I cant seem to have both the search and pagination at the same time.

This gives me the pagination (see below):

def index

@projects = Project.all
@projects = Project.paginate :per_page => 4, :page => params[:page]
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @projects }
end

end

This gives me my search index (see below):

def index

@projects = Project.all
@search = Project.search do
  fulltext params[:search]
end
@projects = @search.results
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @projects }
end

end

But when I add the pagination it doesn't work/display (see below):

def index

@projects = Project.paginate :per_page => 4, :page => params[:page]
@search = Project.search do
  fulltext params[:search]
end
@projects = @search.results
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @projects }
end

end

The search still works but the pagination doesn't appear... Any ideas how I get them both working together?

Many Thanks!


Solution

  • Answering my own question here, never occured to me to add in IF and ELSE to separate the actions in the def index....

    This works perfectly...

    if params[:search]
    @search = Project.search do
      fulltext params[:search]
    end
      @projects = @search.results
    else
    @projects = Project.all
    @projects = Project.paginate(:page => params[:page], :per_page => 4)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @projects }
    end