简体   繁体   中英

How to include javascripts within ruby on rails 5.2.3?

I want to use some code from my old .js files in my current ruby on rails project. The plan is to give some data from rails to javascript libs and then view it. However I can't manage to bring it all together.

I've tried some advices from here and there and from some other sites (like this one , this one and plenty more). But the tips I found were useful for the older versions of rails, and didn't work on rails 5.2.3.

Almost every site has a tip about adding jQuery. But I don't use jQuery in these files. Furthermore rails includes it automatically, so I didn't change anything there.

I feel stuck with this problem. Any help will be appreciated!

Rails 5 has same asset pipeline based on sprockets just like rails 4 does (unlike rails 6 which has webpacker by default), so these tips are valid for your case.

Make sure that your file ends up in the bundle as you expect (sometimes a server restart might be needed to pick up new files) and check browser console for js errors.

Also if you're using turbolinks - instead of document.ready event, listen for turbolinks:load :

$(document).on('turbolinks:load', function() {
  console.log("This runs on each visit")
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM