Search code examples
ruby-on-railsruby-on-rails-3solrsunspot

Error searching multiple models with Sunspot Solr Rails 3


Finally got Sunspot working when searching a single model, but running into some problems when searching multiple.

The error that I'm getting after making a search:

Missing template search/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/Indokathan/code/iibfy/app/views" * "/usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.4.1/app/views"

search.rb

class Search < ActiveRecord::Base
 attr_accessible :title

 searchable do
   text :title      
  end
end

search_controller.rb

class SearchController < ApplicationController

def index
@search = Sunspot.search [Dairy, Drink] do
  fulltext params[:search]
 end
  @results = @search.results
 end  
end

searchbar.html.erb

<%= form_tag search_index_path, :method => :get do %>
<p>
    <%= text_field_tag :search, params[:search], style:"width:550px; height:30px;" %><br>
    <%= submit_tag "Search!", :name => nil, class: "btn btn-primary btn-lg", style: "margin-top:10px" %>
</p>

Any help as to why I am getting this error would be more than appreciated. If you need any more info please let me know.


Solution

  • While doing Single model search, you're rendering the result in same index page of respected controller, for example let's take Article model as in gem document(articles_controller.rb).

    def index
      @search = Article.search do
          fulltext params[:search]
          with(:published_at).less_than(Time.zone.now)
        facet(:publish_month)
        with(:publish_month, params[:month]) if params[:month].present?
      end
      @articles = @search.results
    end
    

    It will render result in same Article index page.

    Since, now you've created a common search controller, you will need to create a separate view for this index action in views>search>index.html.erb and show results in that page or render existing template if that can be used using render :=> "your_view".