Rails 6中Jquery与Bootstrap配置问题求助
Hey there! Let's break down your issue step by step—mixing old and new asset loading syntax is definitely causing confusion, so we'll get this sorted out properly.
First: The Difference Between require("...") and //= require ...
This is the root of your confusion:
require("...")(orimport) is Webpack/ES Module syntax, which is what Rails 6 uses via Webpacker. It's the modern way to bundle JavaScript dependencies.//= require ...is Sprockets syntax, from the old Rails asset pipeline. Webpacker doesn't understand these directives, so mixing them with Webpack's require/import will lead to conflicts and unexpected behavior. You should completely phase out the//= requirelines in your Webpacker-managed files.
Your Current Code Issues
Right now, you're mixing both syntaxes in application.js, and your Webpack config has a small tweak needed. Let's fix this:
Step 1: Clean Up environment.js
Your ProvidePlugin setup is almost right, but we should reference the full jQuery package (not just jquery/src/jquery) and add Popper.js support (since Bootstrap 4 depends on it):
const { environment } = require('@rails/webpacker') const webpack = require('webpack') // Make jQuery, jQuery, and Popper globally available to all modules environment.plugins.prepend('Provide', new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', Popper: ['popper.js', 'default'] // Bootstrap needs this for dropdowns/tooltips }) ) module.exports = environment
Step 2: Rewrite application.js (Remove Old Sprockets Syntax)
Delete all //= require ... lines—they don't belong here. Then structure your imports/requires properly to ensure global jQuery access:
// Explicitly import jQuery and attach it to the window object (safe for global use) import $ from 'jquery' window.$ = $ window.jQuery = $ // Load Rails core functionality require("@rails/ujs").start() require("turbolinks").start() require("@rails/activestorage").start() require("channels") // Load Bootstrap JS import 'bootstrap/dist/js/bootstrap' // Load your custom styles and scripts import '../stylesheets/application' import './bootstrap_custom.js'
- We attach jQuery to
windowto make it available everywhere (including inline scripts in views), which complements the ProvidePlugin setup. - No need for duplicate jQuery requires—this single import is enough.
Step 3: Verify Your Layout
Your application.html.erb layout is already using the correct Webpacker tags (stylesheet_pack_tag and javascript_pack_tag), so no changes needed here!
Step 4: Final Checks
- Restart your Rails server and Webpack dev server (important for config changes to take effect)
- Clear old assets with
rails assets:clobber - Test your app—jQuery should now be globally available in all views, no more need for CDN hacks!
Quick Recap of Your Original Questions
- Do I need both
require("jquery")and//= require jquery3? No—delete the//= require jquery3line entirely. Theimport $ from 'jquery'(orrequire("jquery")) combined with the ProvidePlugin and window attachment is all you need. - Is
//= requireold syntax? Yes—Rails 6 uses Webpacker, so you should avoid Sprockets directives in yourpacksfolder.
内容的提问来源于stack exchange,提问作者Tomas

