Rails: Bootstrap dropdown menu not working

后端 未结 6 1351
逝去的感伤
逝去的感伤 2021-01-24 14:18

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

相关标签:
6条回答
  • 2021-01-24 14:53

    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.

    0 讨论(0)
  • 2021-01-24 14:55

    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 .
    
    0 讨论(0)
  • 2021-01-24 15:00

    you can try to call it via javascript adding :

    <script>
       $('.dropdown-toggle').dropdown()
    </script>
    

    at the botton of your _header file.

    hope helps!

    0 讨论(0)
  • 2021-01-24 15:05

    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

    0 讨论(0)
  • 2021-01-24 15:14

    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 :)

    0 讨论(0)
  • 2021-01-24 15:15

    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!

    0 讨论(0)
提交回复
热议问题