简体   繁体   English

导航栏折叠不适用于 Rails 5/Bootstrap 3

[英]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尝试使用 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应用程序.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:如果您使用的是 Rails 5.1+,那么您还需要在此处添加 jquery,如下所示:

On the application.js update like belowapplication.js更新如下

//= 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.将这些复制并粘贴到页面底部似乎可行,但我认为 Rails 的方法是安装 jquery gem 并将其包含在 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将此添加到 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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