且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Bootstrap 3 下拉菜单不适用于 Rails 4 &涡轮链接

更新时间:2023-01-05 20:01:18

此问题无需使用 jquery-turbolinks 即可解决.您首先需要了解为什么会发生这种情况.当启用 turbolinks 并单击链接时,会触发 page:change 事件,并且所有 javascript 都会从新加载的 html 中重新计算.

This issue can be resolved without using jquery-turbolinks. You need to first understand why this happens. When turbolinks is enabled and you click on a link, a page:change event is fired and all javascript is re-evaluated from the newly loaded html.

这包括在 application.js 中加载的引导 javascript 代码.重新加载引导程序的 javascript 时,它会中断.您可以通过在 application.js 标记中使用 data-turbolinks-eval=false 来解决此问题.

This includes the bootstrap javascript code that's loaded within application.js. When bootstrap's javascript is reloaded, it breaks. You can fix this by using data-turbolinks-eval=false in the application.js <script> tag.

对于 ERB:

<%= javascript_include_tag 'application', 'data-turbolinks-eval' => false %>
<%= javascript_include_tag params[:controller] %>

对于瘦身:

== javascript_include_tag 'application', 'data-turbolinks-eval': false
== javascript_include_tag params[:controller]