bootstrap typeahead不在rails上工作

时间:2013-09-09 03:13:34

标签: ruby-on-rails twitter-bootstrap bootstrap-typeahead

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对我不起作用,我必须遗漏一些东西。

1 个答案:

答案 0 :(得分:-1)

似乎您没有提供data-sourcedata-source应该是一个呈现数组的字符串。像:

<input type="text" data-provide="typeahead" data-source="[&quot;Alabama&quot;,&quot;Alaska&quot;,&quot;Arizona&quot;,&quot;Arkansas&quot;]"/>

这是一个小提琴: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