Search code examples
javascriptjqueryturbolinksruby-on-rails-6easyautocomplete

Rails 6 App links not working until refresh


I tried to follow this tutorial by GoRails to add EasyAutoComplete to my Rails app: https://www.youtube.com/watch?v=ibxlNN73UTY

Although the search bar functions as the GoRails guy demonstrates, I keep seeing this page whenever I click on any link: error page

If I refresh this page, then it takes me to the user sign in page that I originally clicked on. I don't notice anything out of the ordinary in my Rails server console throughout this process.

I added the css files and the js files for EasyAutoComplete to my Assets pipeline, because I was having a hard time using webpacker correctly.

For jquery, I followed this article to install it: https://www.botreetechnologies.com/blog/introducing-jquery-in-rails-6-using-webpacker and I used this video as reference as well: https://www.youtube.com/watch?v=bn9arlhfaXc

My Gemfile looks like:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.0.rc2'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'redis'
gem 'braintree'
gem 'bcrypt'
gem 'foreman'
gem 'rails-controller-testing'
gem 'faker'
gem 'pagy'
gem 'figaro'
gem 'gon'
gem 'modernizr-rails'
gem 'stripe-rails'
# thirdparty authentications
gem 'omniauth-google-oauth2'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'activerecord-session_store'
# for front end
gem 'react-rails'
gem 'bootstrap'
gem 'sprockets-rails'
gem 'jquery-rails'
# catches test emails
gem 'mailcatcher'
gem 'devise'
# admin panel
gem 'rails_admin'

# active storage variant
gem 'mini_magick', '~> 4.8'
gem 'rack', '~> 2.0.8'
gem 'ransack'

My search.js looks like:

document.addEventListener('turbolinks:load', function () {
  $input = $("[data-behavior='autocomplete']")

  var options = {
    getValue: 'name',
    url: function (phrase) {
      return '/search.json?q=' + phrase
    },
    categories: [
      {
        listLocation: 'products',
        header: '<strong>Products</strong>'
      }
    ],
    list: {
      onChooseEvent: function () {
        var url = $input.getSelectedItemData().url
        $input.val('')
        Turbolinks.visit(url)
      }
    }
  }

  $input.easyAutocomplete(options)
})

My application.js in app/javascript/packs looks like:

require('@rails/ujs').start()
require('turbolinks').start()
require('@rails/activestorage').start()
require('channels')
require('jquery')

//= require jquery
//= require popper
//= require bootstrap-sprockets
//= require jquery.easy-autocomplete

Any help would be greatly appreciated!

Edit: When I remove the "require('turbolinks').start()" in my application.js the problem goes away but my search bar stops working.

When I click on a link with my developer console up I see this: enter image description here

I also created a sample app that also has the same problem in this repository: https://github.com/zx1301/sampleAppTurbolinksError I'm using ruby 2.6.2 and rails 6.0.2. In its current state I commented out the lines linking the css and js files to easyAutocomplete and the problem persists unless I disable turbolinks by commenting out turbolinks in application.js.


Solution

  • Going by where the links in the repo you linked navigate to (nowhere #), this is a known issue with turbolinks.

    You can add data-turbolinks="false" to the link tags to prevent this type of behavior.