简体   繁体   English

Rails 5 Bootstrap Javascript 标签不工作

[英]Rails 5 Bootstrap Javascript Tags Not Working

I'm having strange issues with Bootstrap 4 and the Rails 5 javascript tags.我在使用 Bootstrap 4 和 Rails 5 javascript 标签时遇到了奇怪的问题。 Everything seemed to work fine when I started on this project and I haven't made any changes to any of the js, but suddenly my collapsable navbar dropdowns stopped functioning, and I can see these errors in the console:当我开始这个项目并且我没有对任何 js 进行任何更改时,一切似乎都运行良好,但突然我的可折叠导航栏下拉菜单停止运行,我可以在控制台中看到这些错误:

GET http://localhost:3000/packs/js/application-48ce69a491950ec17b29.js net::ERR_ABORTED 404 (Not Found)

GET http://localhost:3000/packs/js/application-48ce69a491950ec17b29.js net::ERR_ABORTED 404 (Not Found)

I am using Rails 5.2.4.1 with Ruby 2.6.3 and Bootstrap 4.我正在使用带有 Ruby 2.6.3 和 Bootstrap 4 的 Rails 5.2.4.1。

In my application.html.erb I have these javascript tags at the end of the body:在我的 application.html.erb 中,我在正文末尾有这些 javascript 标签:

<%= javascript_include_tag 'application' %>

<%= javascript_pack_tag 'application' %>

But I think the problem must be contained in the tags or the files, because when I directly add these Bootstrap script tags into the application.html my dropdowns start working again:但我认为问题必须包含在标签或文件中,因为当我直接将这些引导脚本标签添加到 application.html 我的下拉菜单再次开始工作:

<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

So this is really confusing me and I want to make sure that the include_tag and pack_tag are actually working so that I can use the asset pipeline to keep other js files organised in folders when I need more.所以这真的让我很困惑,我想确保 include_tag 和 pack_tag 确实在工作,这样当我需要更多时,我可以使用资产管道将其他 js 文件组织在文件夹中。

This is my assets/javascripts/application.js:这是我的资产/javascripts/application.js:

//= require rails-ujs
//= require_tree .

And my javascript/packs/application.js:还有我的 javascript/packs/application.js:

import "bootstrap";

And here is my config/webpack/environment.js这是我的 config/webpack/environment.js

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')

// Preventing Babel from transpiling NodeModules packages
environment.loaders.delete('nodeModules');

// Bootstrap 4 has a dependency over jQuery & Popper.js:
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    Popper: ['popper.js', 'default']
  })
)

module.exports = environment

I'm using a Devise template made by the LeWagon bootcamp and am not yet confident of how everything is interacting.我正在使用LeWagon 训练营制作的 Devise 模板,但我对一切如何交互尚无信心。 So here is the gemfile it generated for safe measure:因此,这是它为安全措施而生成的 gemfile:

source 'https://rubygems.org'
ruby '2.6.3'

gem 'bootsnap', require: false
gem 'devise'
gem 'jbuilder', '~> 2.0'
gem 'pg', '~> 0.21'
gem 'puma'
gem 'rails', '5.2.4.1'
gem 'redis'

gem 'autoprefixer-rails'
gem 'font-awesome-sass', '~> 5.12.0'
gem 'sassc-rails'
gem 'simple_form'
gem 'uglifier'
gem 'webpacker'

group :development do
  gem 'web-console', '>= 3.3.0'
end

group :development, :test do
  gem 'pry-byebug'
  gem 'pry-rails'
  gem 'listen', '~> 3.0.5'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'dotenv-rails'
end

Am I forgetting to include any other files that might be affecting this?我是否忘记包含可能影响此的任何其他文件? Hopefully someone can spot a simple problem in here, but happy to add more info if I've missed something.希望有人能在这里发现一个简单的问题,但如果我错过了什么,很高兴添加更多信息。 This is really getting on my goat lol.这真的让我的山羊哈哈大笑。

Thanks in advance!提前致谢!

It's not good idea to have javascript_include_tag 'application' and javascript_pack_tag 'application' at the same time同时拥有javascript_include_tag 'application'javascript_pack_tag 'application'并不是一个好主意

If you use webpacker you don't need app/assets/javascripts folder如果您使用 webpacker,则不需要app/assets/javascripts文件夹

And you need to change app/javascript/packs/application.js to something like this:您需要将app/javascript/packs/application.js更改为如下内容:

require("@rails/ujs").start();
require("@rails/activestorage").start();
require("channels");
require("jquery");

import "bootstrap";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM