Bootstrap Navbar下拉菜单不起作用

时间:2018-12-25 19:08:54

标签: javascript jquery css ruby-on-rails

每当我单击它时,它都会通过在路线的末尾添加“ /#”来重定向我。单击下拉列表时,控制台日志中没有jquery错误。这是我的applicaiton.js:

//
//= jquery
//= jquery-ui
//= popper
//= bootstrap-sprockets
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree

我有JQuery-Rails gem和jquery-ui-rails。当我单击下拉列表时,控制台日志现在显示jquery / javascript错误。这是我的服务器日志:

Started GET "/" for 127.0.0.1 at 2018-12-25 14:06:27 -0500
Processing by StaticController#homepage as HTML
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/sohel/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
  Rendering static/homepage.html.erb within layouts/application
  Rendered static/homepage.html.erb within layouts/application (0.7ms)
  Rendered shared/_nav.html.erb (0.5ms)
Completed 200 OK in 47ms (Views: 41.2ms | ActiveRecord: 0.4ms)

这是导航栏组件的view erb文件。它基本上是直接从引导网站上删除的:

<nav class="navbar navbar-expand-lg navbar-light bg-light custom-nav">
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <%= link_to 'Home', root_path, class: 'nav-link' %>
      </li>
      <li class="nav-item">
        <%= link_to 'Time Entries', posts_path, class: 'nav-link' %>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

2 个答案:

答案 0 :(得分:0)

设置显式href("#")将使您的浏览器更改url片段,您可以将其删除(<a href class="..." ...>),并且浏览器不会尝试遵循它。

<a
  class="nav-link dropdown-toggle"
  href 
  id="navbarDropdownMenuLink" 
  data-toggle="dropdown" 
  aria-haspopup="true" 
  aria-expanded="false"
>
  Dropdown link
</a>

另一方面,下拉菜单应该切换。检查网络检查器,并确保您实际上实际上是在加载应用程序JavaScript。

更新

您的application.js文件中有错误。这些库只是命名而没有require d。它应显示为:

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

(还要注意require_tree行末的点)

答案 1 :(得分:0)

我找到了答案here

必须在我的application.html.erb布局文件的关闭正文标记下添加以下脚本标记:

  <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://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 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>