Timmy Von Heiss Timmy Von Heiss - 3 months ago 7
Javascript Question

How do I install masonry or other javascript in rails? (not using gems)

I wanted Masonry for my Rails 4.2 project and initially installed it via the masonry-rails gem and then wasted an evening until I realized that the gem was using an outdated version of Masonry. This will be my first attempt to add javascript to my Rails app without a gem. From the installation docs the options are: Download, CDN, Package managers (bower/npm)

I used CDN by simply adding

<script src="https://npmcdn.com/masonry-layout@4.1/dist/masonry.pkgd.min.js"></script>


to the top of my index.html.erb view file that will use the script. Is this correct?

What is the advantage/disadvantage of using CDN instead of Download or Package managers and if I want to add
masonry.pkgd.min.js
manually to my project how would i do so?

This has some good related info

Answer

What is the advantage/disadvantage of using CDN instead of Download

To know the pros and cons of CDN vs Local script, see answers here and here

How to manually add them in project

There are 2 ways

  1. Either put them(e.g. masonry.pkgd.min.js) under app/assets/javascripts and require them in application.js as //= require masonry.pkgd.min.js but only if you do not have this line //= require_tree . Because require_tree will load all js files under app/assets/javascripts folder

  2. Or put them in vendor/assets/javascripts. But in this case you must require it in application.js file

added masonry script to the top of index.html.erb view file that will use the script. Is this correct?

It is correct in the sense that it will load only to that particular view. Now if you put the same in application.html.erb or loaded it via assets then the same js script will load on every views.

Comments