user2012677
user2012677

Reputation: 5745

Add JS link to Application.js file

Application.js file

// ** Standard Rails 5 files **
//= require rails-ujs
//= require activestorage
//= require turbolinks
//
// ** Bootstrap JS files **
//= require jquery3
//= require popper
//= require bootstrap-sprockets

I want to add a jquery plugin https://github.com/gdsmith/jquery.easing into my rails app. I would prefer to add a link to the CDN link rather than reference the file directly.

1) How do I add a link to the CDN file in the code above?

2) If I have another file, that is is App/assets/vendor/vendor_name/vendor.js, how do I reference it in the above file?

//=require /assets/vendor/vendor_name/vendor 

did not work.

Upvotes: 0

Views: 909

Answers (1)

Brad
Brad

Reputation: 8678

If you want to link to a CDN just go into views/layout/application.html.erb and add the link to the CDN with a normal <link> tag

An alternative is to simply go to the CDN link in your browser, copy the js code, then in rails create a file in assets/javascript/your_file_name.js, paste the code in there and if you are using require tree . it will get picked up automatically. Personally I don't like require tree so I include them all individually. (be aware this means you will need to manually apply any updates to the library.)

The vendor folder is automatically included in the asset path, you should be able to simply reference it relative to the vendor directory.

e.g. //=require vendor_name/vendor

Note: If rails can't find the file, it will show you on the error screen all the places it looked, one of those will be the vendor path, you can then work out what additional path you need to provide to get to your file.

Hope this helps

Upvotes: 2

Related Questions