Ruby on rails or simply Rails is a wonderful web application framework, written in Ruby. It provides services for a web page designing. It has won tech geeks votes for its faster services in terms of developing a website because of its user-friendly features, reduced complexity, and thus improving its efficiency allover. One such feature for RoR is The Asset Pipeline.
The asset pipeline is a very powerful feature that Rails offers, to solve a wide range of problems related to web designing.
The Asset Pipeline :
The asset pipeline is technically no longer a core feature of Rails 4, it has been extracted out of the framework into the sprockets-rails gem.
The asset pipeline is enabled by default.
We can disable the asset pipeline while creating a new application by passing the –skip-sprockets option.
rails new appname –skip-sprockets
Rails 4 automatically adds the sass-rails, coffee-rails and uglifier gems to your Gemfile, which are used by Sprockets for asset compression:
Using the –skip-sprockets option will prevent Rails 4 from adding sass-rails and uglifier to Gemfile, so if you later want to enable the asset pipeline you will have to add those gems to your Gemfile. Also, creating an application with the –skip-sprockets option will generate a slightly different config/application.rb file, with a require statement for the sprockets railtie that is commented-out. You will have to remove the comment operator on that line to later enable the asset pipeline:
# require “sprockets/railtie”
config.assets.css_compressor = :yui
config.assets.js_compressor = :uglifier
How to Use the Asset Pipeline :
Assets can still be placed in the public hierarchy. Any assets under public will be served as static files by the application or web server when config.serve_static_files is set to true. You should use app/assets for files that must undergo some pre-processing before they are served.
In production, Rails precompiles these files to public/assets by default. The precompiled copies are then served as static assets by the web server. The files in app/assets are never served directly in production.
Controller Specific Assets :
Precompiling Assets :
Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline.
Compiled assets are written to the location specified in config.assets.prefix. By default, this is the /assets directory.
You can call this task on the server during deployment to create compiled versions of your assets directly on the server. See the next section for information on compiling locally.
The rake task is:
$ RAILS_ENV=production bin/rake assets:precompile
Capistrano (v2.15.1 and above) includes a recipe to handle this in deployment. Add the following line to Capfile:
This links the folder specified in config.assets.prefix to shared/assets. If you already use this shared folder you’ll need to write your own deployment task.
It is important that this folder is shared between deployments so that remotely cached pages referencing the old compiled assets still work for the life of the cached page.
Live Compilation :
In some circumstances we may wish to use live compilation. In this mode all requests for assets in the pipeline are handled by Sprockets directly.
To enable this option set:
config.assets.compile = true
On the first request the assets are compiled and cached as outlined in development above, and the manifest names used in the helpers are altered to include the MD5 hash.
Sprockets also sets the Cache-Control HTTP header to max-age=31536000. This signals all caches between your server and the client browser that this content (the file served) can be cached for 1 year. The effect of this is to reduce the number of requests for this asset from your server; the asset has a good chance of being in the local browser cache or some intermediate cache.
This mode uses more memory, performs more poorly than the default and is not recommended.
group :production do
Railscarma has been implementing Ruby on Rails from its nascent stages for development, training, deploying and contributing back to the Rails Community and provide best Ruby on Rails Development Services. RailsCarma provide end to end Ruby on Rails services including, consulting, architecture, building, management and extension to companies around the globe. You can also hire Ruby on Rails developers with an easy to hire process.Contact Us to know more.
Read More :
- What is REST API ?
- Implementing TinyMCE in Ruby on Rails
- The Basics of Creating and using Modules in Rails.
- Polymorphic Associations With Active Record