Search code examples
javascriptruby-on-railscoffeescriptturbolinksruby-on-rails-5

How do I make this snippet of CoffeeScript TurboLinks 5 friendly?


I have the following:

$ ->
    $('[data-provider="summernote"]').each ->
      $(this).summernote
        height: 300

And this works, however, I would like to make it TurboLinks 5 friendly.

So I tried the following:

$ ->
  ready = ->
    $('data-provider="summernote"]').each ->
      $(this).summernote
        height: 300

  $(document).ready(ready)
  $(document).on('page:load', ready)

But I keep getting this error:

Uncaught ReferenceError: ready is not defined

How do I fix this so that I make the original snippet TL-5 friendly?

Edit 1

By the way, I am doing this in my jobs.coffee file, which I know is being included in my asset-pipeline. So that's not it.


Solution

  • In Rails 5 (Turbolinks 5), turbolinks:load. Also, you don't need to wrap your event handlers into the "document ready".

    Rewrite your code to this:

    ready = ->
      $('data-provider="summernote"]').each ->
        $(this).summernote(height: 300)
    
    $(document).ready(ready)
    $(document).on('turbolinks:load', ready)