You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 6中Jquery与Bootstrap配置问题求助

Fixing jQuery & Bootstrap Global Availability in Rails 6 with Webpacker

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("...") (or import) 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 //= require lines 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 window to 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

  1. Restart your Rails server and Webpack dev server (important for config changes to take effect)
  2. Clear old assets with rails assets:clobber
  3. 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 jquery3 line entirely. The import $ from 'jquery' (or require("jquery")) combined with the ProvidePlugin and window attachment is all you need.
  • Is //= require old syntax? Yes—Rails 6 uses Webpacker, so you should avoid Sprockets directives in your packs folder.

内容的提问来源于stack exchange,提问作者Tomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:29:21