Good day!
I use pg_search to implement Full text searching in my web app. I created form where user print his data and then i try to show it but result always empty! What is wrong?
/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: After editing in /controllers/users_controller.rb I have params[:q]. Now i get such error:
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: I created empty partial views/pg_search/documents/_document and now i just don't have any results.
I'm the author of 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!