【问题标题】:Error searching multiple models with Sunspot Solr Rails 3使用 Sunspot Solr Rails 3 搜索多个模型时出错
【发布时间】:2015-05-28 13:15:01
【问题描述】:

终于在搜索单个模型时让 Sunspot 正常工作,但在搜索多个模型时遇到了一些问题。

搜索后出现的错误:

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"

搜索.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

搜索栏.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>

任何关于我为什么会收到此错误的帮助将不胜感激。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 solr sunspot


    【解决方案1】:

    在进行单模型搜索时,您会将结果呈现在相关控制器的同一索引页面中,例如让我们将文章模型与 gem 文档中的一样 (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
    

    它将在相同的文章索引页面中呈现结果。

    因为,现在您已经创建了一个通用搜索控制器,您需要在 views&gt;search&gt;index.html.erb 中为这个 index 操作创建一个单独的视图,并在该页面中显示结果或呈现现有模板(如果可以使用 @) 987654325@.

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多