Will_Paginate with Sunspot - Ruby on Rails

GuidesRuby on RailsWeb Development

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
 'products', :action => 'search', :method => :get do %>
    '25', :class => 'search' %>
#Product display page - index.html.erb

    "products/product_line", :locals => {:product => product} %>

      
 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 comments

Comments from the original blog are preserved here. New comments are closed.

  1. gabocas

    Useful post! Thank you.

  2. Errors
    In reply to gabocas

    No worries, thanks for the feedback

  3. Anthony

    Great post! Thanks for this.

  4. Errors
    In reply to Anthony

    No problem, thanks for the feedback.

  5. Eli

    Thanks for this! Was tearing hair out trying to figure out how to work this with Sunspot...

  6. Errors
    In reply to Eli

    lol no worries, ran into quite a few problems myself :D

  7. abetterwaytogive

    How can I search from my homepage and have sunspot return results on another view?