【问题标题】:Sunspot fulltext search - showing search parameter太阳黑子全文搜索 - 显示搜索参数
【发布时间】:2012-10-05 00:31:16
【问题描述】:

我想显示搜索对象的搜索参数。我使用铁轨和太阳黑子。

假设我有一个图书应用程序,其中存储了一堆图书,并且希望通过它们的名称和标题来搜索图书。

图书模型:

class Book < ActiveRecord::Base
  validates_presence_of :title, :author_name

  searchable do
    text :title, :author_name
  end
end

控制器:

class BooksController < ApplictionController
  def index
   @search = Book.search do
    fulltext params[:search]
  end
   @results = @search.results
  end
end

查看:

<div>
 <h1>You search: </h1>
 <% for result in @results %>
    <li>
      <h3><%= link_to result.title, result %></h3>
      <p><%= result.author_name %></p>
    </li>
 <% end %>
</div>

如何显示搜索参数,该对象在哪里?

所以它输出:

Your search: Jeff Hawkins
Eggs and butter
Jeff Hawkins

【问题讨论】:

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


    【解决方案1】:

    将您的控制器代码更改为:

    def index
      @query = params[:search]
      @search = Book.search do
        fulltext params[:search]
      end
      @results = @search.results
    end
    

    然后在您看来,您可以这样做:

    <h1>Your search: <%= @query %></h1>
    

    【讨论】:

    • 执行搜索时返回的数组是否有一个带有搜索参数的字段?
    • 数组不会 (@results) 因为它只是一个活动记录对象的数组,@search 对象确实如此,但它非常嵌入 - 要从那里获取搜索参数,你需要做这样的事情:@search.query.fulltext.to_params[:q] 进行简单搜索。这将是不可靠的,并且绝对不能抵抗太阳黑子宝石的变化。你最好只得到前面提到的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    相关资源
    最近更新 更多