Will_Paginate with Sunspot – Ruby on Rails

Hey all,

I’ve just gone through setting up will_paginate with sunspot 1.3, just thought I’d share in case anyone else has any troubles/improvements.

In my case I’m setting it up on a products model.

  #products.rb
 
  #Sunspot/Search
  searchable do
    text :title, :boost => 5
    text :description
  end

#products_controller.rb
  #Products search
  def search
 
    #Create vars
    search = Product.search do
      fulltext params[:search]
      paginate :page => params[:page] || 1, :per_page => 2
    end    
 
    #Retrieve results
    @products = search.results
 
    #Create responses
    respond_to do |format|
      format.html { render 'index' }
    end
  end

#Search partial - _search.html.erb
<%= form_tag :controller => 'products', :action => 'search', :method => :get do %>
   <%= text_field_tag :search, params[:search], :size => '25', :class => 'search' %>
<% end %>

#Product display page - index.html.erb
<% @products.each do |product| %>
   <%= render :partial => "products/product_line", :locals => {:product => product} %>
<% end %>
 
<%= will_paginate @products, :container => false, :params => { :search_text => params[:search_text] } %>

Well that’s about it, if anyone needs any further info or has any tips, leave a comment and I’ll get back to you asap.

7 thoughts on “Will_Paginate with Sunspot – Ruby on Rails

Leave a Reply