JQuery in Rails is failing after linking from another page, works on page load

笑着哭i 提交于 2019-11-28 12:51:02

I'd guess that this is Turbolinks related. From the fine manual:

Events

With Turbolinks pages will change without a full reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code. Instead Turbolinks fires events on document to provide hooks into the lifecycle of the page.

AFAIK, Rails4 enables turbolinks by default (and you have it in your application.js) so $(function() { ... }) won't always fire when changing pages. You could try binding to turbolinks:load instead:

$(document).on('turbolinks:load', function() {
    setInterval(rotateQuotes, 5000);
});

You might want to bind to turbolinks:before-visit to clean things up as well.

Alternatively, you could disable Turbolinks if you don't care about it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!