1

再会!

我使用 pg_search在我的网络应用程序中实现全文搜索。我创建了用户打印他的数据的表单,然后我尝试显示它,但结果总是空的!怎么了?

/views/users/index.html.erb

<%= render 'shared/search_form' %>  
<%= will_paginate %>   
  <ul class="users">  
     <%= render @users %>       
  </ul>   
<%= will_paginate %>

/views/shared/_search_form.erb

<% search_id_attr = :q %>
<%= form_tag( users_path, :method => "get" ) do %>
  <%= label_tag( search_id_attr, "Search for:" ) %>
  <%= text_field_tag( search_id_attr ) %>
  <%= submit_tag("Search", :name => nil ) %>
<% end %>

/controllers/users_controller.rb

class UsersController < ApplicationController  
  ...
  def index
    @title = "All users"
    @users = PgSearch.multisearch( params[:q] ).paginate( :page => params[:page] )
  end
...
end

UPD1:在 /controllers/users_controller.rb 中编辑后,我有 params[:q]。现在我收到这样的错误:

ActionView::Template::Error (Missing partial pg_search/documents/document with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
  * "/Users/iUser/rails_projects_terminal/sample_app/app/views"
):
    6: <%= will_paginate %>
    7: 
    8: <ul class="users">
    9:  <%= render @users %>                                                                                                    
    10: </ul>
    11: 
    12: <%= will_paginate %>
  app/views/users/index.html.erb:9:in `_app_views_users_index_html_erb__3107215974202561754_70174719087800'

UPD2:我创建了空的部分 视图/pg_search/documents/_document,现在我没有任何结果。

4

2 回答 2

2

我是 的作者pg_search

You ought to rename your variable from @users to @pg_search_documents, because PgSearch.multisearch always returns objects of type PgSearch::Document, not User.

Then you will want to do something like this in your view:

<%= render 'shared/search_form' %>  
<%= will_paginate %>   
<ul class="users">
  <%= @pg_search_documents.each do |pg_search_document| %>
    <%= render pg_search_document.searchable %>  
  <%= render @users %>       
</ul>   
<%= will_paginate %>

This is because multisearch was designed for searching across multiple models.

If you want to make a search that only searches User, then you should instead use pg_search_scope to build a search scope directly on the User class. For example:

class User < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_by_name, :against => [:name]
end

User.search_by_name("ExR") # => returns an Active Record scope of `User` objects.

Let me know if this isn't clear. I know that the documentation has grown a bit large and should be simplified and organized so that developers have a better chance of understanding the subtleties.

Thanks!

于 2012-01-15T07:06:48.957 回答
0

params[:Search]将包含您的提交按钮的名称。我想你的意思是params[:q]。试试看。

于 2012-01-06T15:02:21.857 回答