Rails 6 个应用程序链接在刷新之前无法使用

Rails 6 App links not working until refresh

我尝试按照 GoRails 的教程将 EasyAutoComplete 添加到我的 Rails 应用程序: https://www.youtube.com/watch?v=ibxlNN73UTY

尽管搜索栏的功能与 GoRails 人员演示的一样,但每当我单击任何 link 时,我都会看到此页面:

如果我刷新此页面,则会转到我最初单击的用户登录页面。在整个过程中,我的 Rails 服务器控制台没有发现任何异常。

我将 EasyAutoComplete 的 css 文件和 js 文件添加到我的资产管道中,因为我很难正确使用 webpacker。

对于jquery,我是按照这篇文章安装的:https://www.botreetechnologies.com/blog/introducing-jquery-in-rails-6-using-webpacker 我也用这个视频作为参考: https://www.youtube.com/watch?v=bn9arlhfaXc

我的 Gemfile 看起来像:

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'

我的 search.js 看起来像:

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)
})

我在 app/javascript/packs 中的 application.js 看起来像:

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

如有任何帮助,我们将不胜感激!

编辑: 当我删除 application.js 中的 "require('turbolinks').start()" 时,问题消失了,但我的搜索栏停止工作了。

当我在打开开发者控制台的情况下单击 link 时,我会看到:

我还创建了一个示例应用程序,它在此存储库中也有同样的问题: https://github.com/zx1301/sampleAppTurbolinksError 我正在使用 ruby 2.6.2 和 rails 6.0.2。在当前状态下,我将 link 行 css 和 js 文件注释掉到 easyAutocomplete 并且问题仍然存在,除非我通过注释掉 turbolinks 来禁用 turbolinks在 application.js.

经过您 linked 导航到的回购中 links 的位置(无处 #),这是 turbolinks 的一个已知问题。

您可以将 data-turbolinks="false" 添加到 link 标签以防止此类行为。