在Rails中集成Bootstrap Tour报错:无法找到text/css类型的bootstrap-tour文件
Let's break down how to resolve this error and get Bootstrap Tour working smoothly in your Rails app:
First, Understand the Root Cause
The error pops up because Sprockets (Rails' asset pipeline) can’t locate a plain CSS file named bootstrap-tour when using the *= require directive in your SCSS manifest. The bootstrap-tour-rails gem packages its styles as SCSS files, not raw CSS—so we need to use SCSS’s native @import instead.
Step 1: Fix the Stylesheet Import
Open your application.scss file and replace the problematic require directive with an SCSS import:
/* * This is a manifest file that'll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. * * You're free to add application-wide styles to this file and they'll appear at the bottom of the * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * *= require_tree . *= require_self */ // Replace the *= require bootstrap-tour line with this import @import "bootstrap-tour";
Step 2: Fix JavaScript Loading Order
Looking at your application.js, you’re loading bootstrap-tour before bootstrap-sprockets—this can cause bugs since Bootstrap Tour depends on Bootstrap’s core components (like modals or tooltips). Adjust the order to load Bootstrap first:
// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. JavaScript code in this file should be added after the last require_* statement. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require turbolinks //= require popper //= require tether //= require bootstrap-sprockets //= require bootstrap-tour //= require_tree .
Step 3: Verify and Restart
- Save both modified files
- Restart your Rails server (this ensures the asset pipeline picks up the changes properly)
- Clear your browser cache or do a hard refresh (
Ctrl+Shift+RorCmd+Shift+R) to load the updated assets
Why This Works
- Using
@importin SCSS tells the preprocessor to look for SCSS files in the gem’s asset directories, which matches howbootstrap-tour-railspackages its styles. - Loading Bootstrap’s JS first ensures all dependencies that Bootstrap Tour relies on are available when it initializes.
内容的提问来源于stack exchange,提问作者csandrid

