Working twitter-typeahead example?

故事扮演 提交于 2019-12-29 05:35:14

问题


I'm trying to install the twitter-typeahead-rails gem into my app. I've followed several different tutorials but all of them result in errors.

Does anyone have a working example of this gem?


回答1:


Specify gem as dependency in your Gemfile:

# Gemfile

gem 'bootstrap-multiselect-rails'

Require typeahead files in your manifest:

// app/assets/javascripts/application.js

//= require twitter/typeahead
//= require twitter/typeahead/bloodhound

Javascript:

// app/assets/javascripts/models_controller.js

// initialize bloodhound engine
var bloodhound = new Bloodhound({
  datumTokenizer: function (d) {
    return Bloodhound.tokenizers.whitespace(d.value);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace,

  // sends ajax request to /typeahead/%QUERY
  // where %QUERY is user input
  remote: '/typeahead/%QUERY', 
  limit: 50
});
bloodhound.initialize();

// initialize typeahead widget and hook it up to bloodhound engine
// #typeahead is just a text input
$('#typeahead').typeahead(null, {
  displayKey: 'name',
  source: bloodhound.ttAdapter()
});

// this is the event that is fired when a user clicks on a suggestion
$('#typeahead').bind('typeahead:selected', function(event, datum, name) {
  doSomething(datum.id);
});

View:

<-- app/views/models/whatever.html.erb -->

<input type="text" id="typeahead">

Routes:

# config/routes.rb

get 'typeahead/:query' => 'models#typeahead'

Controller:

# app/controllers/models_controller.rb

def typeahead
  render json: Model.where(name: params[:query])
end

## note:  the above will only return exact matches.
## depending on the database being used,
## something else may be more appropriate.
## here is an example for postgres
## for case-insensitive partial matches:

def typeahead
  render json: Model.where('name ilike ?', "%#{params[:query]}%")
end

GET request to /typeahead/%QUERY returns json in the form of:

[
  {
    "name": "foo",
    "id": "1"
  },
  {
     "name": "bar",
     "id": "2"
  }
]



回答2:


The accepted answer is not completely correct.

There seem to be 2 different gems doing roughly the same thing:

bootstrap-multiselect-rails is currently on version 0.9.9 in the gem repository and has a different asset require structure as mentioned in the post. This gem's assets need to be included as:

In application.js:
//= require bootstrap-multiselect

In application.css:
*= require bootstrap-multiselect

More on Git: https://github.com/benjamincanac/bootstrap-multiselect-rails

Alternatively, there is the twitter-typeahead-rails gem, currently on version 0.11.1 which seem to need to be used and included as described in the rest of the accepted answer.

More on Git: https://github.com/yourabi/twitter-typeahead-rails

Both gems seem to be last updated about 5-6 months ago at the moment of writing this.

Finally, the remote URL specified in the Bloodhound JS is incorrect:

remote: '/typeahead/%QUERY'

Needs to be

remote: {url: '/typeahead/%QUERY', wildcard: '%QUERY'}

Hopefully this helps someone



来源:https://stackoverflow.com/questions/28102146/working-twitter-typeahead-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!