【问题标题】:Searchkick highlight: undefined method `with_details' for []:ArraySearchkick 亮点:[]:Array 的未定义方法“with_details”
【发布时间】:2016-09-12 14:12:05
【问题描述】:

我正在尝试使用 SearchKick 在我的应用中实现摘录突出显示,但 Rails 一直告诉我我得到了错误的对象类型。

我的控制器:

def search

  @articles = Article.text_search(params[:q])
  ...

我的看法:

- articles.with_details.each do |article, details|
  ...
  p.mb-15.excerpt
    = details[:highlight][:content]

我的模特:

searchkick highlight: [:content]

def self.text_search(query)
  if query.present?
    search(
           query,
           fields:
              [
               "title^10",
               "h1^5",
               "meta_description",
               "content"
              ],
           limit: 5,
           highlight: {
           fields: {content:
               {fragment_size: 100}
             }
           }
       )
  else
    []
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 elasticsearch searchkick


    【解决方案1】:

    我猜你从模型中的这个 else 分支得到 [] 作为你的 text_search 结果:

    else
      []
    end
    

    所以也许你可以在你的视图和控制器中做这样的事情来避免这样的错误:

    控制器:

    articles = Article.text_search(params[:q])
    @detailed_articles = articles.empty? ? [] : articles.with_details
    

    查看:

    - @detailed_articles.each do |article, details|
      ...
      p.mb-15.excerpt
        = details[:highlight][:content]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      相关资源
      最近更新 更多