Rails不会从link_to在Chome中加载js [英] Rails not loading js in Chome from link_to
问题描述
我的rails应用程序在通过link_to helper发送到页面后似乎不会加载某些javascript行为。这发生在Chrome浏览器,但不是IE浏览器(这让我很难说)。我已经看到Chrome不会在bootstrap-switch的多个页面中加载行为,以及我在源代码树中的某些自定义函数。这些将会在我从Chrome重新加载页面后起作用,但不会超出初始link_to。
这是我的application.js:
// =需要jquery
// =需要jquery_ujs
// =需要turbolinks
/ / = require_tree。
// =需要引导程序
// =需要引导程序开关
// =需要jquery-placeholder
// =需要jquery_nested_form
Chrome是如何从link_to helper接收或呈现javascript的方式存在一些已知限制?请让我知道我是否可以提供任何有助于解决此问题的其他信息。
我可以从你的 application.js
使用 turbolink 。
使用turbolink时, $(document).ready(function(){..
)在点击内部链接时被忽略,因为页面不会以传统方式实际重新加载。 b
你有两个选择,重写/重构你的JS,以便它可以很好地与turbolinks搭配(通过没有页面特定的JS包括,通过适当地确定你的事件等等),或者你可以禁用turbolink如果你不想使用它:
- 从你的
Gemfile
- 从
application.js
中移除// = require turbolinks
- 在您的
版面中删除对
。data-turbolinks-track=> true
application.html.erb
My rails application does not seem to be loading some javascript behavior after being sent to a page via the link_to helper. This is happening in Chrome, but not IE (which hurts me to say). I've seen Chrome not loading behavior in multiple pages for bootstrap-switch, and for some custom functions that I have in my source tree. These will work after I reload the page from Chrome, but not off of the initial link_to.
Here is my application.js:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap
//= require bootstrap-switch
//= require jquery-placeholder
//= require jquery_nested_form
Is there some known limitation in how Chrome receives or renders javascript from the link_to helper? Please let me know if I can provide any additional information that will be helpful to resolve this.
I can see from your application.js
that you're using turbolinks.
When using turbolinks, $(document).ready(function() { ..
is ignored when clicking on internal links because the page never actually reloads in the traditional way.
You have a couple of alternatives, rewrite/restructure your JS so that it plays nicely with turbolinks (by not having page-specific JS includes, by scoping your events appropriately, etc) or you can disable turbolinks if you don't want to use it by:
- removing it from your
Gemfile
- remove the
//= require turbolinks
from yourapplication.js
- remove the 2 references to
"data-turbolinks-track" => true
in yourlayouts/application.html.erb
.
这篇关于Rails不会从link_to在Chome中加载js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!