pg_search和Rails 3.2.3中Multisearch的基本设置 [英] Basic Setup for Multisearch in pg_search and Rails 3.2.3

查看:110
本文介绍了pg_search和Rails 3.2.3中Multisearch的基本设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我经过数月潜伏和吸收之后的第一个问题。所以我希望我能够正确地做到这一点。



在学习了pg_search_scope功能之后,我一直试图在我的Rails 3.2.3应用程序中获得pg_search的multisearch功能。 a Railscast 。这个Railscast 。我相信pg_search文档假定读者比我有更好的Rails工作知识。我只是无法从我发现的资源中跳跃到使用multisearch获得有效应用程序。任何帮助将非常感激。这是我的设置:

config / initializers / pg_search.rb

  PgSearch.multisearch_options = {
:using => {
:tsearch => {
:dictionary => english
},
:trigram => {}
},
:忽略=> :重音
}

视图中的搜索表单

 <%= form_tag articles_path,方法:: get do%> 
<%= text_field_tag:query,params [:query],:class => 搜索框%>
<%= submit_tag搜索此网站,名称:nil,:class => btn btn-search%>
<%end%>

article.rb

  include PgSearch 
multisearchable:against => [:title,:content]

def self.search(query)
if query.present?
search(query)
else
scoped
end
end

articles_controller.rb

  def索引
@articles = PgSearch.multisearch(params [:query])

respond_to do | format |
format.html#index.html.erb
format.json {render json:@articles}
end
end

在搜索已知字词时,我不会收到搜索结果。我做错了什么?

解决方案

看起来我的错误是使用 @articles 在我的控制器中的变量,而不是显式定义 @pg_search_documents ,这是我在我的视图中使用的(我完全忘了发布)。出于某种原因,我认为在我的控制器中使用 @articles = PgSearch.multisearch(params [:query])会通过pg_search魔术将搜索结果附加到@pg_search_documents酱。

This is my very first question here, after many months of lurking and absorbing. So I hope I do this correctly.

I have been trying to get the multisearch functionality of pg_search working in my Rails 3.2.3 app after learning about the pg_search_scope functionality from this Railscast. I believe that the pg_search documentation assumes that the reader has a better working knowledge of Rails than I do. I just haven't been able to make the jump from the resources I've found to getting a working app using multisearch. Any help would be much appreciated. Here's my setup:

config/initializers/pg_search.rb

PgSearch.multisearch_options = {
  :using => {
    :tsearch => {
      :dictionary => "english"
    },
    :trigram => {}
  },
  :ignoring => :accents
}

Search Form in the View

<%= form_tag articles_path, method: :get do %>
    <%= text_field_tag :query, params[:query], :class => "search-box" %>
    <%= submit_tag "Search This Site", name: nil, :class => "btn btn-search" %>
<% end %>

article.rb

include PgSearch
multisearchable :against => [:title, :content]

def self.search(query)
  if query.present?
    search(query)
  else
    scoped
  end
end

articles_controller.rb

def index
  @articles = PgSearch.multisearch(params[:query])

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @articles }
  end
end

I get no search results when searching for known terms. What am I doing wrong?

解决方案

Looks like my error was using @articles variable in my controller instead of explicitly defining @pg_search_documents, which is what I was using in my view (which I completely forgot to post). For some reason I thought that using @articles = PgSearch.multisearch(params[:query]) in my controller would append the search results to `@pg_search_documents' via the pg_search magic sauce.

这篇关于pg_search和Rails 3.2.3中Multisearch的基本设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆