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
@usersto@pg_search_documents, becausePgSearch.multisearchalways returns objects of typePgSearch::Document, notUser.Then you will want to do something like this in your view:
This is because
multisearchwas designed for searching across multiple models.If you want to make a search that only searches
User, then you should instead usepg_search_scopeto build a search scope directly on theUserclass. For example: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!