rails and bootstrap: Uncaught ReferenceError: jQuery is not defined
Assuming that after adding gem 'jquery-rails'
, you have ran bundle install
command.
Make sure you have following in app/assets/javascripts/application.js
//= require jquery//= require jquery_ujs//= require bootstrap.min // Add it after jquery and jquery_ujs
EDIT
You will need to explicitly include jQuery before including /bootstrap.min.js
. For example :
<!DOCTYPE html><html><head> <title>Bootsrap Test 01</title> <link rel="stylesheet" href="/css/bootstrap.css"> <script src="/assets/jquery.js" type="text/javascript"></script> <script src="/assets/jquery_ujs.js" type="text/javascript"></script> <script src="/js/bootstrap.min.js" type="text/javascript"></script></head>
Even if you have set jquery correctly you can still see that issue when you inline javascript code is evoked before jQuery is initialized.
You can wrap your javascript code in
document.addEventListener("DOMContentLoaded", function(event) { //do work with $});
Or refactor your code to not use inline javascript :)
on 'application.js' of rails 5.0.1 the default is
//= require bootstrap//= require jquery//= require jquery_ujs//= require turbolinks//= require_tree .
change it to
//= require jquery//= require jquery_ujs//= require turbolinks//= require_tree .//= require bootstrap
*load first jquery before bootstrap