All threads / jquery.turbolinks not working on my app

Ask A Question

Notifications

You’re not receiving notifications from this thread.

jquery.turbolinks not working on my app

Enrique Benitez asked in Javascript

I'm using both jquery and turbolinks on my app and because turbolinks don't make full page loads I need some of my js to fire when navigating between pages using turbolinks, I really don't want to use :data-no-turbolink => true because what I want to do is the same as GoRails Twitter share button on the bottom of my app pages, and if I don't do a full page refresh the Twitter widget is not going to render.

So I installed jquery.turbolinks and require it on my application.js file and that's not working, I dunno why, I even tried to do it without jquery.turbolinks by using $(document).on("ready page:load)" and inside of it the Twitter widget js code and nothing happens.

How does GoRails uses turbolinks and still renders the footer social buttons?

Any solutions?

Thanks in advance! =)

I got it working now, I used a different approach, I added <script src="//platform.twitter.com/widgets.js"></script> into the body, so in that way I use an external script tag within the body and now my Twitter share/follow buttons renders correctly along with turbolinks :)

Also, what stopped jquery.turbolinks and event listeners from working correctly was a different story, I got some console errors about a gem I installed, called gon, the error: gon is not defined every time I navigated from page to page using turbolinks, somehow NONE of my js code was working, I believed that was causing the problem (maybe the gon js code never terminated executing?) but I fixed it and now jquery.turbolinks and my js code is all good.

It was so simple to fix, but got my head spinning for like 5 straight hours! LOL

Thanks anyway! =)

Awesome, glad you got it working! Turbolinks can definitely be tricky to wrap your head around what's going on.

Also for reference, this is a pretty great list of solutions to convert JS from popular libraries to Turbolinks compatible stuff: http://reed.github.io/turbolinks-compatibility/twitter.html

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,558+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.