Assets comprise a big part of a web application. All the stuff other than the standard HTML code, fall into the category of assets, like images, audio files, video files, js files, and css files. Sometimes, it is us who write the CSS, and JS code, and sometimes we get this code from 3rd party services for free or for a fixed cost. Whatever is the case, Rails provides us a flexible way to handle those assets.
Rails basically has three ways to handle assets:
All the assets that are written by the developer(s) of the web application reside inside app/assets directory. Put in other words, these are the assets that just belong to your application, and will only fit in there. It usually contains code to successfully manipulate and stylize the pages in your application, and without these assets, your application might not work correctly.
lib/assets contains assets that fit into their own libraries, but have been written by the developer of the web application. For example, let’s say that your web application needs a snow-fall animation, and you write a lot of code for it. At the end, you package up that code into a minfied js file, but since it has been written by you, and not by any 3rd party company, so you put it inside lib/assets. lib/assets contains independent libraries of code that is written by the same developers who develop your application.
Finally, vendor/assets contains the assets written by 3rd party services. They may be free or paid ones, but in any case, you put them inside vendor/assets directory. A good use case for vendor/assets directory is Bootstrap.
We can manually download each JS or CSS file that our project needs, and put it inside vendor/assets directory or there is a better way for handling 3rd party stuff, and that is to use gems.
For example, in order to use jQuery inside our project, we often use jquery-rails. The benefit of using a gem over manually downloading and putting the files in vendor/assets directory is that we don’t have to care about the latest version of the asset files.
If we update jquery-rails gem, it’s automatically gonna get the latest version of the respective library, and gonna handle all the dependencies for us. So let gems take care of the things that otherwise, we would do manually.
So among all the magic features of Rails, asset management is an important one. We can easily handle code written by us, and written by the third parties. But handing over the manual doing task to gems is a great way of increasing productivity and developing an application that is easy to handle. How do you usually handle managing assets for 3rd parties, let us know in the comment section below.