-1

I can use the search bar in every pages but I can not use autocomplete until refresh the page.

You can find "layouts/header.html.erb" , "User model" , "users_controller" , "routes.rb" in this link Autocompleted search form is not working in all pages

Gemfile

 gem 'rails', '4.2.4'
 gem 'bcrypt', '~> 3.1.7'
 gem 'faker',                '1.4.2'
 gem 'carrierwave',             '0.10.0'
 gem 'mini_magick',             '3.8.0'
 gem 'fog',                     '1.36.0'
 gem 'will_paginate',           '3.0.7'
 gem 'bootstrap-will_paginate', '0.0.10'
 gem "searchkick"

 gem 'uglifier', '>= 1.3.0'

 gem 'jquery-rails'
 gem 'jquery-ui-rails'

 gem 'turbolinks'

 gem 'jbuilder', '~> 2.0'

 gem 'sdoc', '~> 0.4.0', group: :doc

 gem 'bootstrap-sass', '~> 3.2.0'
 gem 'autoprefixer-rails'
 gem 'make_flaggable', :git => 'git://github.com/medihack/make_flaggable'

application.html.erb

 <head>
     <title><%= yield(:title) %> | Blogger </title>
     <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
     <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

     <!-- Using it for autocomplete --> 

     <%= javascript_include_tag "//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js" %>

     <!--         -->

      <      %= csrf_meta_tags %>
            <%= render 'layouts/shim' %>
 </head>

layouts/shim

<!--[if lt IE 9]>
   <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
   </script>
<![endif]-->

user.js.coffee

 $ ->
    $('#user_search').typeahead
       name: "user"
       remote: "/users/autocomplete?query=%QUERY"
Community
  • 1
  • 1
Takor
  • 277
  • 2
  • 12

2 Answers2

0

" The thing about using turbolinks is that most plugins and libraries that bind to the document ready event stop working, since turbolinks prevents the browser from reloading the page. "

The answer is from this link. JQuery gets loaded only on page refresh in Rails 4 application

Thanks for all.

Community
  • 1
  • 1
Takor
  • 277
  • 2
  • 12
0

I had the same issue with rails 5 and Jquery Autocomplete. I had my search box in my navbar and every time i went to a different page in my app my autocomplete with elasticsearch would break, that is not bring up all users associated with what i was typing. To fix I added this to my _nav.html.erb <nav class="navbar navbar-fixed-top navbar-inverse" data-turbolinks="false">. I tried it in my div just around the search box but, that didn't seem to work but, worked in the nav. Per the Turbolinks page: With Turbolinks, these events will fire only in response to the initial page load—not after any subsequent page changes".

max56
  • 101
  • 1
  • 3