简体   繁体   中英

Navbar Collapse not working on Rails 5/Bootstrap 3

When the navbar menu changes to a dropdown menu, the button to access the menu is unresponsive.

I have tried:

  • reordering the code
  • doing a rake assets:clean
  • trying with JS

And still can't figure out how to fix it. Here's my code:

    <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="main-nav-collapse" aria-expanded="false">
        <span class="sr-only">Toggle Navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <%= link_to root_path, class: 'navbar-brand' do %>
      <img src="https://i.imgur.com/TtTQgbx.png" alt="minilogo" class="minilogo">
      <% end %>
    </div>
    <div class="collapse navbar-collapse" id="main-nav-collapse">
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to 'Book Club', '/bookclub' %></li>
        <li><%= link_to 'Musings', '/musings' %></li>
        <li><%= link_to 'Podcasts', '/podcasts' %></li>
        <li><%= link_to 'Resources', '/resources' %></li>
        <li><%= link_to 'Courses', '/courses' %></li>
        <li><%= link_to 'About', about_path %></li>

      </ul>
    </div>
  </div>
</nav>

application.js

//= require bootstrap-sprockets
//= require jquery
//= require rails-ujs
//= require turbolinks
//= require_tree .
//=

Gemfile

gem 'jquery-rails', '4.3.1'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'

Try the following

If you are using Rails 5.1+ then you will need to add jquery here as well, so it would look like below:

On the application.js update like below

//= require rails-ujs
//= require jquery
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree .

Hope it helps

Copying and pasting these at the bottom of the page seems to work, though i think the rails way would be to install the jquery gem and include it in application.js.

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

Add this to the application.html

<!-- jQuery first, then Tether, then Bootstrap JS. -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM