搜索太阳黑子(facet)

时间:2014-05-18 10:19:19

标签: ruby-on-rails search ruby-on-rails-4 sunspot

我正在关注此视频广播#278 Search with Sunspot以实施与太阳黑子的分面搜索,但我收到此错误undefined method facet'为nil:NilClass`

这是我的文章模型

   searchable do
      text :title, boost: 4
      text :content
      time :created_at
      string :publish_month
    end

    def publish_month
      created_at.strftime("%B %Y")
    end

这是我的搜索控制器

def搜索

  @articles = Sunspot.search(article) do
    fulltext params[:query]
    with(:created_at).less_than(Time.zone.now)
    facet(:publish_month)
    with(:publish_month, params[:month]) if params[:month].present?
    end

    respond_to do |format|
      format.html { render :action => "search" }
    end
  end

这是我对方面的看法

<div id="facets">
  <h3>Published</h3>
  <ul>
    <% for row in @search.facet(:publish_month).rows %>
      <li>
        <% if params[:month].blank? %>
          <%= link_to row.value, :month => row.value %> (<%= row.count %>)
        <% else %>
          <strong><%= row.value %></strong> (<%= link_to "remove", :month => nil %>)
        <% end %>
      </li>
    <% end %>
  </ul>
</div>

1 个答案:

答案 0 :(得分:0)

你有没有机会在你的视图中提到@articles而不是@search?

尝试将该行更改为:

<% for row in @articles.facet(:publish_month).rows %>
相关问题