Rails javascript only works after reload
Solution 1:
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();
})
Solution 2:
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)