Bootstrap 3 dropdown doesn't work with Rails 4 & Turbolinks

旧巷老猫 提交于 2019-11-29 13:20:51

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.

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.

For ERB:

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

For SLIM:

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

I answered this question over at: turbolinks issues with bootstrap modal

In short: you need the jquery-turbolinks gem.

I was able to solve the problem by adding "data-turbolinks-eval" => false to my Bootstrap CDN link.

<%= javascript_include_tag 
  "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js", 
  "data-turbolinks-eval" => false 
%>

You are not using turbolinks the good way. The turbolinks should not be used with assets. The method of using turbolinks can be seen here: Handling Turbolinks and JS incompatibility in Rails

You should not change any javascript_links. Just in App/assets/javascripts/application.js put code like:

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//
// ... your other scripts here ...
//
//= require turbolinks

after putting jquery-turbolinks in Gemfile. The bootstrap require should come in between jquery.turbolinks and turbolinks.

See this answer on twbs github: https://github.com/twbs/bootstrap-sass/issues/714

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