Getting Typeahead.js, Searchkick, and Bootstrap 3 to work in Rails 4

I tried to get this to work for the last two days, to no avail. I use Bootstrap 3 and I tried both versions of typeahead: http://twitter.imtqy.com/typeahead.js/releases/latest/typeahead.jquery.js

and older version:

http://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js . And this CSS:

https://raw.github.com/grk/bookstore-example/master/app/assets/stylesheets/typeahead.css

My javascript is as follows:

  $( "#user_search").typeahead({
      name: "user",                                                   
      remote: "/users/autocomplete?query=%QUERY"
    })

My model:

class User  
  include Mongoid::Document
  field :user_id, type: Integer
  field :name, type: String

  searchkick autocomplete: ['name']

In my controller, I:

  def autocomplete
    render json: User.search(params[:query], autocomplete: true, limit: 10).map(&:name)
  end 

Config / routes:

  resources :users, only: :index do
    collection do
      get :autocomplete
    end
  end

  resources :users

And my opinion:

<%= text_field_tag :query, params[:query], class: 'form-control', type: 'text', placeholder: 'Search For User...', id: "user_search", autocomplete: "off" %>

I use Searchkick's autocomplete results, which work great when I do:

User.search("the", autocomplete: true).map(&:name)

rails , URL REST (/users/autcomplete? query = SomeQuery).

typeahead. ; , , ( ). - , ?

+4

Source: https://habr.com/ru/post/1532692/


All Articles