tessad
tessad

Reputation: 1219

sunspot searching

I know I had this search function working only a moment ago but have been redoing my search and navbar with bootstrap and now it looks good but the search function is not working!! I think it's something in the index action? All it does is, well, nothing. Perhaps it's not submitting the form? - how can I check this?

VIEWS *guidelines_controller.rb*

 def index
    @guidelines = Guideline.order(:title).all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @guidelines }
    end
    @search = Sunspot.search(Guideline) do  
    fulltext params[:search]  
  end  
  @guidelines = @search.results 
  end

layouts/application.html.erb

<%= form_tag guidelines_path, :class => 'navbar-search', :method => :get do %>  

    <%= text_field_tag :search, params[:search], :class => 'search-query', :placeholder=>"Search" %> 

MODELS guideline.rb

searchable do
    text :title, :default_boost => 2
    text :subtitle
  end

Upvotes: 1

Views: 486

Answers (1)

basgys
basgys

Reputation: 4400

I am not a Sunspot expert, but your controller method is a bit strange. Why do you render your view before calling Sunspot?

Moreover, why do you fetch all guideline rows (Guideline.order(:title).all) ?

I have refactored your method assuming you want to search with Sunspot if a search condition (params[:search]) is given. Otherwise all rows are fetched.

def index
  if params[:search].present?
    @search = Sunspot.search(Guideline) do  
      fulltext params[:search]
    end

    @guidelines = @search.results
  else
    @guidelines = Guideline.order(:title).all
  end

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @guidelines }
  end
end

By the way, if the Guideline model contains a lot of rows, you should consider limiting the number of rows (limit or pagination).

EDIT

Here is a screencast about Sunspot #278 Search with Sunspot

Maybe you forgot to reindex your search engine?

rake sunspot:reindex

Upvotes: 1

Related Questions