Twitter Typeahead 未在 Rails 4 中显示建议

Twitter Typeahead is is not showing suggestions in Rails 4

我在我的 rails 项目中使用了带有 searchkick 包装器的 elasticsearch。我已成功将 Twitter Typeahead(远程)集成到项目中,但没有提出建议。任何人都可以调查一下这个问题。

我收到了回复,但 typeahead 没有在建议中显示。

下图


回应

控制器:

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()
});

我没有使用预先输入的库,但我认为这里的问题 displayKey: 'name'rails 中的 autocomplete 操作应该提供 json 是什么意思一个键name,但是你渲染了一个数组。尝试从 Product.search(params[:query], fuzziness: 10).

中删除 .map(&:name)