1
我正在使用searchkick gem進行彈性搜索,typeahead用於自動完成。我有一個類似位置,城市,store_name等列的模型商店。我想在自動完成下拉列表中顯示位置和城市。我模型代碼是自動完成結果顯示未定義Typeahead在Rails中
class Store < ActiveRecord::Base
searchkick autocomplete: ['location','city'],
suggest: ['location','city']
attr_accessible :city, :location, :name, :pincode
end
我的控制器是
class StoresController < ApplicationController
# GET /stores
# GET /stores.json
def index
if params[:query].present?
@stores = Store.search(params[:query],fields: [:location,:city])
else
@stores = Store.all
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @stores }
end
end
def autocomplete
render json: Store.search(params[:query], autocomplete: true,fields: [:location,:city], limit: 10).map {|store|
{location: "#{store.location} , #{store.city}"}}
end
end
我的視圖代碼是
<%= form_tag stores_path, class: "form-inline", method: :get do %>
<div class="input-group input-group-lg">
<% if params[:query].present? %>
<div class="input-group-btn">
<%= link_to "clear", stores_path, class: "btn btn-default" %>
</div>
<% end %>
<%= text_field_tag :query, params[:query], class: "form-control typeahead", id: "store_search", autocomplete: "off" %>
<div class="input-group-btn">
<%= submit_tag "Search", class: "btn btn-primary" %>
</div>
</div>
<% end %>
我store.js.coffee
$ ->
$('#store_search').typeahead
name: "store",
displayKey: 'location',
remote: "/stores/autocomplete?query=%QUERY"
當我搜索,返回的結果就像 [{ 「位置」: 「RT納加,班加羅爾」},{ 「位置」: 「RT納加,班加羅爾」 },{「location」:「RT Nagar,Kolkata」}]
我想在自動完成下拉列表中顯示結果。請告訴我如何編寫搜索以獲得確切結果。