sometimes javascript run perfect and sometimes not in ruby on rails 4

谁说我不能喝 提交于 2019-12-05 13:29:06

It looks like turbolinks problem which are added to rails 4 by default. In turbolinks $(document).ready is not always firing. You can see more information about turbolinks and some suggestions how to use them here railscast or you can add to some links

data: { no_turbolink: true }

You can also just remove the turbolinks from gemfile if you don't need them. Also check if you have the ui-icons_ef8c08_256x240.png in

/assets/images/
Khanetor

You can also follow the instruction here. Basically you have to tell javascript to also execute on page:load.

Credit to Meltemi

Coffee:

ready = ->

  ...your coffeescript goes here...

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

Javascript:

var ready;
ready = function() {

  ...your javascript goes here...

};

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

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

First, install jquery-turbolinks gem. And then, don't forget to move your included Javascript files from end of body of your application.html.erb to its <head>.

As described here, if you have put the application javascript link in the footer for speed optimization reasons, you will need to move it into the tag so that it loads before the content in the tag. This solution worked for me.

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