1

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

Rahul Singh
  • 3,417
  • 2
  • 25
  • 32
Timmy Von Heiss
  • 2,160
  • 17
  • 39

1 Answers1

1

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.

Community
  • 1
  • 1
Rahul Singh
  • 3,417
  • 2
  • 25
  • 32
  • he also asked whether his use of CDN in index.html.erb by calling `` is correct or not. I'm interested in this point, can you pls add info to your answer? – davideghz Aug 07 '16 at 15:54
  • @davideghz I added my opinion whether adding embedded js is good or bad. It may be different for different persons. – Rahul Singh Aug 07 '16 at 16:06