0

I am trying to create a search form where a user will be able to enter text and the backend will run PgSearch.multisearch in order to retrieve values from a Customer and Account model.

I have set up my two models according to the pg_search gem's page. When I run a PgSearch.multisearch("foo") in my rails console it returns results as expected.

My question is how do I display all relevant results in my view? (Can't seem to figure this one out)

Currently my controller looks like this (params[:search][:token] == "foo"):

def create
   if @search.validate(params[:search])
      @pg_search_documents = PgSearch.multisearch(params[:search][:token])
      respond_with(@pg_search_documents, :location => search_index_path)
    else
      respond_with(@search, :location => search_index_path)
    end
end

In my view I tried:

- @pg_search_documents.each do |pg_search_document|
  = pg_search_document.searchable.id

I keep on getting an error stating "undefined method `each' for nil:NilClass"

What I'd like to achieve is to display all results in my view.

UPDATE 2

Running PgSearch.multisearch("foo") in console returns:

#<ActiveRecord::Relation [#<PgSearch::Document id: 6, content: "foo", searchable_id: 10, searchable_type: "Customer", created_at: "2015-01-20 22:29:31", updated_at: "2015-01-20 22:29:31">]>

When I render PgSearch.multisearch(params[:search][:token]) as text in my browser it only shows a "#".

I figured that this is normal behaviour for a ActiveRecord::Relation, as the same output structures occur when I try to output the results of a AR scope in console vs browser.

UPDATE 3

enter image description here

Can somebody please provide some help?

4

0 回答 0