2015-11-25 34 views
0

我在我的rails項目中使用了searchsearch包裝器的elasticsearch。我已經成功地將Twitter Typeahead(遠程)整合到項目中,但建議並未提出。任何人都可以請看這個問題。Twitter Typeahead是沒有在Rails 4中顯示建議

我得到迴應,但typeahead沒有在建議中顯示它。

圖片下面

enter image description here


響應

enter image description here

控制器:

def autocomplete 
    render json: Product.search(params[:query], fuzziness: 10).map(&:name) 
end 

index.html.erb:

<%= form_tag products_path, method: :get do %> 
    <ul> 
    <li class="list"> 
     <%= text_field_tag :query, params[:query],placeholder: "Search Here", class: "typeahead form-control", id: "product-search", autocomplete: "off" %> 
    </li> 
    <li class="list"> 
     <%= submit_tag "Search", class: "btn btn-primary btn-size" %> 
    </li> 
     </ul> 
<% end %> 

products.js

var names = new Bloodhound({ 
    datumTokenizer: function(d) { 
    return Bloodhound.tokenizers.whitespace(d.name); }, 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    remote: { 
     url: '/products/autocomplete?query=%QUERY', 
     wildcard: '%QUERY' 
    } 
    }); 

    // initialize the bloodhound suggestion engine 
    names.initialize(); 

    // instantiate the typeahead UI 
    $('#product-search').typeahead(null, { 
    displayKey: 'name', 
    source: names.ttAdapter() 
}); 
+0

其實我是Elasticsearch和Rails的初學者。非常感謝您的回答。是的,它有效:D –

回答

1

我不是一個預輸入的lib工作,但我假設這裏的問題是displayKey: 'name'rails中的autocomplete操作應該提供jsonname,但是您呈現的是數組。嘗試從Product.search(params[:query], fuzziness: 10)刪除.map(&:name)

相關問題