Rails Include JavaScripts Assets Folder Recursively

Rails include javascripts assets folder recursively

//= require_tree .

will require everything in the current directory of your application.js

//= require_tree ./js_library

will require everything in the js_library subdirectory if it is under app/assets/javascripts

If you are are trying to load javascripts under vendor/assets/javascripts, try:

//= require_tree ../../../vendor/assets/javascripts/js_library

rails 4 asset_pipeline : Include whole folder for vendor/assets

You can use the require_tree directive in the application.css and application.js manifest files, which will recursively include any files in the given directories. These are relative paths however, so you'll need something like this:

 *= require_tree ../../../vendor/assets/stylesheets

config.assets.precompile - include a folder of files? or kill the precompile 'feature' entirely?

A good practice when dealing with multiple CSS/JS files to add to the asset pipeline is to simply create a new manifest for those files:

Let's say you have some JS files under lib/assets/javascripts/external/calendars and you want to load them through the asset pipeline.

You want to create an index.js manifest file with the following content:

// 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 vendor/assets/javascripts of plugins, if any, 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
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require_tree .

This way all JS files you add into the external/calendars directory will be included by default thanks to the require_tree . directive.

Now, in your app/assets/javascripts/application.js file add the following line:

//= require calendars

This should find your "calendars' manifest index file" and load all dependent JS files. No need to add anything into the asset pipeline, it will just work.

Sprockets can't find my js files in a folder

Simple fix

require_tree ./global

How to fetch assets inside vendor subfolders in Rails?

First add the /vendor/plugins directory to the assets load path:

module MyApp
class Application < Rails::Application
config.assets.paths << Rails.root.join("vendor", "plugins")
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
end
end

However adding a directory to the assets load path does not mean that Sprockets will search all the subdirectories recursively. Nor is it a very good idea to configure it to do so.

You still need to provide a complete path from /vendor/plugins.

app/assets/javascripts/application.js:

//= require ckeditor/js/Chart

app/assets/stylesheets/application.css:

*= require ckeditor/css/chart.min

Or you can just use the Rails integration gem and skip all the hassle.

`application.js` no longer including all scripts in app/assets/javascripts for Rails 3.1 app

Turns out that I needed to include modernizr before other js includes:

//= require jquery
//= require jquery_ujs
//= require subdirectory/modernizr
//= require_self
//= require_tree .

I'm now using a defined structure for including javascripts vs allowing the manifest to load them all up in default order based on the tree.

Rails 3.1: The public directory no longer serves js assets. How to load an additional js file after page is loaded?

Every asset in the Sprockets load path is accessible at runtime. You can see your load path in Rails console with

Rails.application.config.assets.paths

You can add load paths in an initializer:

Rails.application.config.assets.paths << your_load_path

By default, all assets in apps/assets/ and vendor/assets/ are loaded automatically. These assets must be located inside directories. Files in the assets/ directory itself are not loaded.

You can load an asset located at apps/assets/javascripts/myscripts/hello.js by visiting http://mydomain.com/assets/myscripts/hello.js.

Concatenation is a separate concern. Sprockets will look in the load path for assets that that you specify using the Sprockets require and provide directives and build concatenated files from them.



Related Topics



Leave a reply



Submit