Rails javascript only works after reload
For turbolinks 5.0 you must use the turbolinks:load event, which is called the first time on page load and every time on a turbolink visit. More info: https://github.com/turbolinks/turbolinks#running-javascript-when-a-page-loads
CoffeeScript code:
$(document).on 'turbolinks:load', -> my_func()
JavaScript code:
document.addEventListener("turbolinks:load", function() { my_func();})
This was a turbolinks problem. Thanks to @zwippie for leading me in the right direction!The solution was to wrap my coffeescript in this:
ready = ->// functions$(document).ready(ready)$(document).on('page:load', ready)
I guess this is a turbolinks issue.
Either remove turbolinks from your project or modify your script to something like:
$(function() { initPage();});$(window).bind('page:change', function() { initPage();});function initPage() { // Page ready code...}
As mentioned here.