如何在Rails 4.2.6应用中使用通配符预编译资产?
Hey there, let’s work through this asset precompilation issue you’re hitting. I’ve run into similar headaches with Rails 4.x’s asset pipeline, so here’s a breakdown of what’s going wrong and how to fix it:
Why Your Current Setup Isn’t Working
First, let’s unpack the two problems:
- The
['*.js', '*.css']wildcard only matches files directly in the root ofapp/assets/stylesheets/andapp/assets/javascripts/—it doesn’t recursively look into your module subdirectories. - The
Sass::SyntaxErroris almost certainly because your wildcard is picking up partial files (those starting with an underscore, like_base.scss). Partials are meant to be@imported into other stylesheets, not compiled on their own. They often depend on variables/mixins from other files that aren’t loaded when you compile them standalone.
Solution 1: Use Recursive Wildcards (and Exclude Partials)
If you want to stick with wildcards, you need to target files in subdirectories explicitly and avoid partials. Update your config/initializers/assets.rb like this:
# Keep default precompile rules, then add our recursive matches config.assets.precompile += [ # Match all non-partial JS/CSS in root and 1-level subdirs '*.js', '*.css', '*/*.js', '*/*.css', # Add more levels if you have nested subdirs (e.g., */*/*.js) ] # OR, a more dynamic approach to cover all nested subdirs (avoids hardcoding levels) config.assets.precompile += Dir[Rails.root.join('app/assets/stylesheets/**/[^_]*.css')] config.assets.precompile += Dir[Rails.root.join('app/assets/javascripts/**/[^_]*.js')]
The [^_]* pattern ensures we only pick files that don’t start with an underscore (so no partials). Using += instead of = is critical here—it preserves Rails’ default precompile rules for fonts, SVGs, etc., which you’d lose if you overwrite the array entirely.
Solution 2: Use Manifest Files (Recommended)
The cleaner, more Rails-idiomatic approach is to use manifest files for each module. This aligns with how the asset pipeline is designed and avoids partial-related errors:
Create a manifest for each module:
For example, if you have anadminsubdirectory in stylesheets, makeapp/assets/stylesheets/admin_manifest.csswith:// Import all partials in the admin module @import "admin/base"; @import "admin/dashboard"; @import "admin/users";Do the same for JS:
app/assets/javascripts/admin_manifest.jswith//= require admin/*(or explicit requires if you prefer).Precompile only the manifests:
Updateconfig/initializers/assets.rbto target these top-level manifest files:config.assets.precompile += [ 'admin_manifest.css', 'admin_manifest.js', 'frontend_manifest.css', 'frontend_manifest.js' # Add other module manifests here ]
This way, you’re only compiling the files that are meant to be standalone, and partials are properly imported where they belong—no more Sass syntax errors.
Quick Troubleshooting Tip
If you still get Sass errors, double-check that none of the files you’re precompiling are partials. Run this command to see exactly which files Rails is trying to precompile:
RAILS_ENV=production rake assets:precompile --trace
The trace will show you the specific file causing the error, so you can adjust your precompile rules accordingly.
内容的提问来源于stack exchange,提问作者Steven Aguilar

