I know there are a million questions already on this, but I can't get this.
I want to include most of my JS files in the asset pipeline, but I have a few I want to load conditionally (or only on certain pages). These are big, complicated files and will never, ever be used by 95% of the users, so I'd rather not have them loaded for every user. One set of JS files is for a calendar, placed in:
app/assets/javascripts/calendar
So my manifest is set up to include only the top directory (and exclude the calendar subdirectory):
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory .
My config/environments/production.rb:
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
# This following config is left over from previous Rails app,
# so not sure if it's now unnecessary ...
# Disable Rails's static asset server
# In production, Apache or nginx will already do this
config.serve_static_assets = false
In the view, I'm using Ryan Bates' nifty_layout to manually include the calendar files:
javascript "calendar/date.js", "calendar/jquery.weekcalendar.js", "calendar/custom.js"
I've tried precompiling in both development and production -- the docs aren't clear where I'm supposed to do this, but it looks like production.
And when I run the page, I get this:
ActionView::Template::Error (calendar/date.js isn't precompiled)
I don't want it precompiled. I want it loaded manually. (Actually, it would be OK to precompile in a file other than the main application.js that is created, but I don't know how to do that.)
What's the solution?
Thanks!
To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.
rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .
The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.
rake assets:clean Only removes old assets (keeps the most recent 3 copies) from public/assets . Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.
OK, I didn't realize this was how it works, but I think I figured it out.
Add the files to be manually loaded to config/environments/production.rb like so:
config.assets.precompile += %w( calendar/*.js jquery_calendar/*.css )
I thought this just folded them into the application.js and application.css, but apparently not -- it compiles them as individual files.
Then, you can call the files as you traditionally would (in this case, using nifty_layout):
javascript "calendar/date.js", "calendar/jquery.weekcalendar.js", "calendar/custom.js"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With