English 中文(简体)
如何使用pg_search多搜索功能制作一个包含结果链接的搜索结果页面?
原标题:How to make a search results page with pg_search multisearch that has links to the results?

我终于弄清楚了如何实现pg_search的多搜索功能。但我在制作一个可用的搜索结果页面时遇到了困难,该页面显示了包含搜索词的各种文章和常见问题的链接。这是一个使用Rails 3.2.3的非常基本的设置:

型号:

我有一个Articles和一个Faqs模型,都有“Title”和“Content”属性,这两个模型中的代码如下:

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

搜索表单视图代码:

搜索表将一切传递给一个叫做“结果”的控制器。

<%= form_tag result_index_path, method: :get do %>
  <%= text_field_tag :query, params[:query] %>
  <%= submit_tag "GO", name: nil %>
<% end %>

结果控制者:

class ResultController < ApplicationController
  def index
     @pg_search_documents = PgSearch.multisearch(params[:query])
  end
end

我想制作一个搜索结果页面,显示找到的每个结果的标题,并链接回该项目。我弄清楚了如何从@pg_search_documents中提取class属性。我的想法是在结果页面上做这样的事情:

<ul>
<% @pg_search_documents.each do |pg_search_document| %>
  <li><%= link_to pg_search_document.searchable.title, "../#{pg_search_document.searchable.class}/#{pg_search_document.searchable.id}" %></li>
<% end %>
</ul>

这段代码产生的一个示例链接是:http://localhost/Article/3密码如果我能弄清楚如何将“pg_search_document.research.class”小写并复数化,我就可以自由了。我尝试在模型和控制器中编写各种方法,并尝试编写一个助手。但这就是我的Rails技能让我失败的地方。

有什么建议吗?有人知道一种更优雅的方法来实现这一点吗?任何想法/建议都非常感谢。

最佳回答

我做了类似的事情,只是用了

<%= link_to pg_search_document.searchable.title, pg_search_document.searchable %>

让Rails动态创建相关记录的路径。

问题回答

好吧,暂时摆脱这个问题真是太神奇了。还有对基本Ruby的更持久的谷歌搜索。以下是我提出的解决方案:

<ul>
  <% @pg_search_documents.each do |pg_search_document| %>
    <li><%= link_to pg_search_document.searchable.title, "../#{(pg_search_document.searchable.class).to_s.downcase!.pluralize}/#{pg_search_document.searchable.id}" %></li>
  <% end %>
</ul>

尽管如此,这对我来说似乎很丑陋。我仍然很想看到一些更流线型、更智能的东西。





相关问题
Acronyms with Sphinx search engine

how can i index acronyms like m.i.a. ? when i search for mia , i get results for mia and not m.i.a. . when i search for m.i.a. , i get nothing at all. edit: solution looks roughly like: ...

Querying multiple index in django-sphinx

The django-sphinx documentation shows that django-sphinx layer also supports some basic querying over multiple indexes. http://github.com/dcramer/django-sphinx/blob/master/README.rst from ...

Adding Search to Ruby on Rails - Easy Question

I am trying to figure out how to add search to my rails application. I am brand new so go slow. I have created a blog and done quite a bit of customizing including adding some AJAX, pretty proud of ...

Searching and ranking short phrases (e.g. movie titles)

I m trying to improve our search capabilities for short phrases (in our case movie titles) and am currently looking at SQL Server 2008 Full Text Search, which provides some of the functionality we ...

Will Full text search consider indexes?

Ok I have a full text search index created on my JobsToDo table, but what I m concerned about is if this is rendering my other indexes on the table useless. I have a normal nonclustered index on the ...

Lucene.NET on shared hosting

I m trying to get Lucene.NET to work on a shared hosting environment. Mascix over on codeproject outlines here how he got this to work on godaddy. I m attempting this on isqsolutions. Both ...

Hibernate Search or Compass

I can t seem to find any recent talk on the choice. Back in 06 there was criticism on Hibernate Search as being incomplete and not being ready to compete with Compass, is it now? Has anyone used both ...