Purplrplr
Purplrplr

Reputation: 11

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!

Upvotes: 0

Views: 229

Answers (1)

Vasfed
Vasfed

Reputation: 18504

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")
});

Upvotes: 1

Related Questions