Rails 4: how to use $(document).ready() with turbo-links

前端 未结 19 1578
忘了有多久
忘了有多久 2020-11-21 06:52

I ran into an issue in my Rails 4 app while trying to organize JS files \"the rails way\". They were previously scattered across different views. I organized them into separ

19条回答
  •  感情败类
    2020-11-21 07:51

    As per the new rails guides, the correct way is to do the following:

    $(document).on('turbolinks:load', function() {
       console.log('(document).turbolinks:load')
    });
    

    or, in coffeescript:

    $(document).on "turbolinks:load", ->
    alert "page has loaded!"
    

    Do not listen to the event $(document).ready and only one event will be fired. No surprises, no need to use the jquery.turbolinks gem.

    This works with rails 4.2 and above, not only rails 5.

提交回复
热议问题