Skip to content

Instantly share code, notes, and snippets.

@ljones140
Last active October 11, 2015 14:06
Show Gist options
  • Save ljones140/5680d652ab5e244e6838 to your computer and use it in GitHub Desktop.
Save ljones140/5680d652ab5e244e6838 to your computer and use it in GitHub Desktop.
Rails Turbolinks affecting JS and Foundation
On my instagram clone I used foundation for the styling and created a topbar element with a links to the index page, logon, and sign up.
There was a strange issue that If I clicked on any of the links on the page the page would reload with the top
part of the page being under the topbar. Also I would get rails JS errors if my like link (which uses JS) was clicked.
The strange thing however was that I refresh the page or go to the url directly I would not have an issue.
After googling around I discovered the issue was caused by Turbo links.
From what I understand Turbo links tries to make your website act like a single page app however this messes around
with JQuery as it loads the page in a differen way froma normal refresh so JQuery $(document).ready does not get called.
This stopped my JS from working and Foundation also has it's own JS/Jquery for making things look nice etc.
http://stackoverflow.com/questions/17600093/rails-javascript-not-loading-after-clicking-through-link-to-helper
To resolve my Javascript likes.js and application.js I had to change the function so that document would be ready on ready and page:load
var ready = function() {
//code that does the stuff here
};
$(document).ready(ready);
$(document).on('page:load', ready);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment