I installed my application using a search gem and am working on an autocomplete function for my Leading index. My problem is that I am looking at several fields (first_name and last_name), and although I can search for a presenter in both fields, for example, if I have Presenter 'Jennifer Hall', I can either type โJenniferโ or โHall "'and the name" Jennifer Hall "will appear in the autocomplete drop-down list, but if I type" Jennifer "(as soon as I add a space), the name will disappear and there are no more offers. My question is: how can I fix this?
My code
I follow this guide:
Adding search and autocomplete to a Rails application using Elasticsearch
presenter.rb:
class Presenter < ActiveRecord::Base
searchkick autocomplete: ['first_name', 'last_name'],
suggest: ['first_name', 'last_name']
...
end
presenters_controller.rb:
class PresentersController < ApplicationController
def index
if params[:query].present?
@presenters = Presenter.search(params[:query],
fields: [:first_name, :last_name],
page: params[:page])
else
@presenters = Presenter.all.page params[:page]
end
end
def autocomplete
render json: Presenter.search(params[:query],
autocomplete: true,
fields: [:first_name, :last_name],
limit: 10).map { |presenter|
presenter.first_name + " " + presenter.last_name
}
end
...
end
I got .map {| lead | etc.} from here:
how can I match multiple attributes in rails with the desired stone
However, when I followed the solution exactly, I got "undefined" from the drop-down list of suggested names. Changing it to presenter.first_name + "" + presenter.last_name made the full name appear in the drop-down list.
routes.rb:
resources :presenters, :only => [:index, :show] do
collection do
get :autocomplete
end
index.html.haml:
.container
= form_tag presenters_path, method: :get do
- if params[:query].present?
.btn-search.btn-clearsearch
= link_to "clear", presenters_path
- else
.search
Search
= text_field_tag :query, params[:query],
id: "presenter_search",
autocomplete: "off"
= submit_tag "go", class: "btn-search"
I use typeahead.js, so it works with:
application.html.haml:
= javascript_include_tag 'application'
= javascript_include_tag
"//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js"
and
presenter.js.coffee:
$ ->
$('#presenter_search').typeahead
name: "presenter"
remote: "/presenters/autocomplete?query=%QUERY"
, , , , , , ( , ), , . , , " ". , /, :
.map(&:first_name, &:last_name)
.map(&:first_name, :last_name)
.map{ |presenter| presenter.slice(:first_name, :last_name) }
"undefined."
, ?