The Sprockets asset pipeline helps organize and prepare your website's CSS, JavaScript, and images so they load faster and work well together.
Sprockets asset pipeline in Ruby on Rails
//= require filename
// This directive includes another JavaScript or CSS file into the current one.Use //= require in JavaScript or CSS manifest files to include other files.
Files are processed and combined in the order of these directives.
//= require jquery //= require_tree .
require_self includes the current file's styles before adding styles from custom.css./*
*= require_self
*= require custom
*/This example shows how to use Sprockets directives in CSS and JavaScript files. The CSS manifest includes its own styles and then the custom styles. The JavaScript manifest includes jQuery and all other JS files in the folder. When the app runs, styles and scripts are combined and ready to use.
/* app/assets/stylesheets/application.css */ /* *= require_self *= require custom */ /* app/assets/stylesheets/custom.css */ body { background-color: #f0f0f0; } // app/assets/javascripts/application.js //= require jquery //= require_tree . // app/assets/javascripts/custom.js console.log('Custom JS loaded');
Always restart your Rails server after adding new assets to see changes.
Use rake assets:precompile to prepare assets for production.
Sprockets processes files in the order you specify, so order matters.
Sprockets helps combine and prepare CSS, JavaScript, and images for your Rails app.
Use //= require and *= require directives to include files.
This makes your app faster and easier to manage.