0
votes

I have been using solr search and now I am adding pagination via the will_paginate gem. Loading the initial page works with the pagination links on the bottom of the webpage rendering correctly and working fine, but when I do a search via Solr the pagination links do not get updated. It always loads the 155 pagination links for the non-search compared to at most the 2 pagination link for a search. They retain the old links from the initial page load.

books.rb:

  def self.search(query,page)
    search = Sunspot.search(Book) do
      keywords query
      paginate :page => page, :per_page => 10
    end
    search.results
  end

books_controller.rb:

  def index
    if params[:query] && !params[:query].blank?
      @books = Book.search(params[:query],:page => params[:page])
    else
      @books = Book.order_by(:title).paginate(:page => params[:page], :per_page => 10)
    end
  end

index.html.erb:

  <%= form_tag books_path, :method => :get, :remote => true, :id => "book_search" do  %>
    <div><%= label_tag :query, "Search" %> <%= text_field_tag "query", "" %></div>
    <div><%= submit_tag "Find", :class => "" %></div>
  <% end %>
  <div><%= will_paginate @books %></div>

I have both the will_paginate and sunspot gems installed.

Thanks

1

1 Answers

0
votes

First of all, you can shorten the line

if params[:query] && !params[:query].blank?

into just

if params[:query].present?

as it will accomplish the same thing.

Are you sure you are hitting the correct code branch and not running the Book.order_by line? and are you sure you are testing it with different values of the page parameter?