Rails 5 - turbolinks 5,some JS not loaded on page render

前端 未结 7 1088

I have a Rails app, I recently updated to 5.0.0.RC1. Most of the transition went smooth, but I\'m having some trouble with the new Turbolinks. In my app I for e

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 08:50

    With turbolinks the javascript is only loaded once. On the hard refresh chosen works because the entire page is re-rendered. Once you click a link, turbolinks hijacks the request and turns it into an ajax one (instead of a full page refresh). Once the request comes in, turbolinks only replaces the body of the page, leaving the JS in the same state it was.

    To fix this you will need to wrap your chosen initializer in the turbolinks:load event like so

    $(document).on('turbolinks:load', function() {
      $('#screen-selection').chosen({
        width: '190px'
      })
    })
    

    For more information, see https://github.com/turbolinks/turbolinks#installing-javascript-behavior

提交回复
热议问题