myController的:
@products = Product.all
MyView的:
<%= javascript_tag "var products = #{@products.to_json};" %>
<%= form_for @dolist, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.text_field :product, autofocus: true, placeholder: "product", autocomplete: "off", id: "products", data: {provide: "typeahead"} %>
<div class="form-actions">
<%= f.submit 'Submit', :class => 'btn btn-primary' %>
</div>
<% end %>
typeahead对我不起作用,我必须遗漏一些东西。
答案 0 :(得分:-1)
似乎您没有提供data-source
,data-source
应该是一个呈现数组的字符串。像:
<input type="text" data-provide="typeahead" data-source="["Alabama","Alaska","Arizona","Arkansas"]"/>
这是一个小提琴:http://jsfiddle.net/bigxiang/fTNwg/
在您看来,如果格式正确,您可以使用@products.to_json
。
在复杂情况下,您可以直接使用javascript:
<%= f.text_field :product, autofocus: true, placeholder: "product", autocomplete: "off", id: "products"%>
<%= javascript_tag "var products = #{@products.to_json};" %>
<script type="text/javascript">
$('#products').typeahead({
source: products
#other options like events
});
</script>
请参阅文档:http://getbootstrap.com/2.3.2/javascript.html#typeahead