Getting started with Ruby on RailsRoutingActiveRecordViewsActiveRecord MigrationsRails Best PracticesNaming ConventionsActionCableActiveModelUser Authentication in RailsActiveRecord AssociationsActiveRecord ValidationsActiveRecord Query InterfaceActionMailerRails generate commandsConfigurationI18n - InternationalizationUsing GoogleMaps with RailsFile UploadsCachingActionControllerConfigurationSafe ConstantizeRails 5Authorization with CanCanMongoidGemsChange default timezoneAsset PipelineUpgrading RailsActiveRecord LockingDebuggingConfigure Angular with RailsRails loggerPrawn PDFRails APIDeploying a Rails app on HerokuActiveSupportForm HelpersActiveRecord TransactionsRSpec and Ruby on RailsDecorator patternElasticsearchReact with Rails using react-rails gemRails Cookbook - Advanced rails recipes/learnings and coding techniquesMultipurpose ActiveRecord columnsClass OrganizationShallow RoutingModel states: AASMRails 5 API AutheticationTesting Rails ApplicationsActive JobsRails frameworks over the yearsAdd Admin PanelNested form in Ruby on RailsFactory GirlImport whole CSV files from specific folderTools for Ruby on Rails code optimization and cleanupActiveJobActive Model SerializersRails Engine - Modular RailsSingle Table InheritanceActiveRecord TransactionsTurbolinksFriendly IDSecurely storing authentication keysAuthenticate Api using DeviseIntegrating React.js with Rails Using HyperloopChange a default Rails application enviornmentReserved WordsRails -EnginesAdding an Amazon RDS to your rails applicationPayment feature in railsRails on docker

Asset Pipeline

Other topics

Rake tasks

By default sprockets-rails is shipped with the following rake tasks:

  • assets:clean[keep]: Remove old compiled assets
  • assets:clobber: Remove compiled assets
  • assets:environment: Load asset compile environment
  • assets:precompile: Compile all the assets named in config.assets.precompile

Manifest Files and Directives

In the assets initalizer (config/initializers/assets.rb) are a few files explicitly defined to be precompiled.

# Precompile additional assets.
# application.coffee, application.scss, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )

In this example the application.coffee and application.scss are so called 'Manifest Files'. This files should be used to include other JavaScript or CSS assets. The following command are available:

  • require <path>: The require directive functions similar to Ruby's own require. It provides a way to declare a dependency on a file in your path and ensures it's only loaded once before the source file.
  • require_directory <path>: requires all the files inside a single directory. It's similar to path/* since it does not follow nested directories.
  • require_tree <path>: requires all the nested files in a directory. Its glob equivalent is path/**/*.
  • require_self: causes the body of the current file to be inserted before any subsequent require directives. Useful in CSS files, where it's common for the index file to contain global styles that need to be defined before other dependencies are loaded.
  • stub <path>: remove a file from being included
  • depend_on <path>: Allows you to state a dependency on a file without including it. This is used for caching purposes. Any changes made to the dependency file will invalidate the cache of the source file.

An application.scss file could look like:

/*
 *= require bootstrap
 *= require_directory .
 *= require_self
 */

Another example is the application.coffee file. Here with including jquery and Turbolinks:

#= require jquery2
#= require jquery_ujs
#= require turbolinks
#= require_tree .

If you don't use CoffeeScript, but plain JavaScript, the syntax would be:

//= require jquery2
//= require jquery_ujs
//= require turbolinks
//= require_tree .

Basic Usage

There are two basic ways that the asset pipeline is used:

  1. When running a server in development mode, it automatically pre-processes and prepares your assets on-the-fly.

  2. In production mode, you’ll probably use it to pre-process, versionize, and compress and compile your assets. You can do so by running the following command:

    bundle exec rake assets:precompile

Contributors

Topic Id: 3386

Example Ids: 11648,11649,32747

This site is not affiliated with any of the contributors.