2016-05-30 78 views
0

用戶指定城市。自動完成應通過輸入的第一個字母進行。不要使用twitter-typeahead-rails工作自動填充

如果在字段中輸入第一個字母,例如「Ber ...」,則什麼也不會發生。

我如何爲搜索和自動填充城市運行腳本?

我specifed寶石作爲依賴於Gemfile中:

gem 'twitter-typeahead-rails' 

的application.js

//= require jquery 
//= require jquery.turbolinks 
//= require underscore 
//= require json2 
//= require judge 
//= require registration 
//= require jquery-ui 
//= require tag-it 
//= require jquery_ujs 
//= require chosen 
//= require cocoon 
//= require jquery-fileupload/basic 
//= require twitter/typeahead 
//= require freelancers 
//= require response 
//= require projects 
//= require cities_germany 
//= require turbolinks 

資產/ JavaScript的/ cities_germany.js

var cities = new Bloodhound({ 
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('num'), 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    local: [{ city: 'Berlin (Berlin)'}, 
      { city: 'Hamburg (Hamburg)'}, 
      { city: 'München (Bavaria)'}, 
      { city: 'Köln (Northrhine-Westfalia)'}, 
      { city: 'Frankfurt (Hessen)'}, 
      { city: 'Essen (Northrhine-Westfalia)'}, 
      { city: 'Dortmund (Northrhine-Westfalia)'}, 
      { city: 'Stuttgart (Baden-Württemberg)'}, 
      { city: 'Düsseldorf Northrhine-Westfalia'}, 
      { city: 'Bremen (Bremen)'}, 
      { city: 'Hannover (Lower Saxony)'}, 
      { city: 'Duisburg (Northrhine-Westfalia)'}, 
      { city: 'Nürnberg (Bavaria)'}, 
      { city: 'Leipzig (Saxony)'}, 
      { city: 'Dresden (Saxony)'} 
      ] 
}); 

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

$('#main-cities .typeahead').typeahead(null, { 
    displayKey: 'city', 
    source: cities.ttAdapter() 
}); 

我在加形成html nex T字段:

<li class="form-fields__group form-fields__group_full"> 
       <%= f.label 'City' %> 
       <div class="field" id="main-cities"> 
       <%= f.text_field :location, class: 'typeahead' %> 
       </div> 
      </li> 

回答

0

我成立錯誤:

替換numbers.initialize()來cities.initialize();和空格('num')到空格('city')