I am quite new to Rails and have been following the tutorial by Michael Hartl. Everything has been going along really well until I got to the drop down menu, which was wasn\'t
What I was able to determine was the issue was 2 rouge js files that I had placed in the assets/javascript directory. Namely, bootstrap.js and bootstrap.min.js. Once I removed these files the dropdown was working again.
Using a later version of rails (5.2.3) I found that the instructions given in the book didn't work. The console warned me that jQuery needed to be installed, and by going to https://github.com/rails/jquery-rails I was able to find the instructions to get it up-and-running.
Add to the Gemfile:
gem 'jquery-rails'
Add to application.js
//= require jquery
//= require bootstrap
So that the whole Gemfile is
//= require rails-ujs
//= require activestorage
//= require jquery
//= require bootstrap
//= require turbolinks
//= require_tree .
you can try to call it via javascript adding :
<script>
$('.dropdown-toggle').dropdown()
</script>
at the botton of your _header file.
hope helps!
The problem is bootstrap's javascript. Sometimes is loaded two times.
This works to me:
application.js:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
application.scss: Don't use //= require in scss files
@import "*";
@import "bootstrap";
@import "font-awesome";
Then:
rake assets:clean
rake assets:precompile
OR
Put bootstrap.js in assets/javascripts folder and put //= require_tree . line
I changed the order of directives in app/assets/javascripts/application.js to:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
Worked like a charm :)
In modern version of the book (rails 6+) this is what i've done to make it work:
In app/javascript/packs/application.js change the order of
require("jquery");
import "bootstrap";
to be before turbolinks - so final implementation of application.js looks like:
require("jquery");
import "bootstrap";
require("@rails/ujs").start();
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");
Book shows to add jquery & bootstrap at the end after turbolinks - what in my opinion causes the problem. Thanks!