简体   繁体   English

Bootstrap 导航栏下拉菜单不起作用 Rails

[英]Bootstrap Navbar dropdown not working Rails

Whenever I click on it it just redirects me by adding a '/#' to the end of my route.每当我点击它时,它都会通过在我的路线末尾添加一个“/#”来重定向我。 There are not jquery errors in the console log when I click the drop down.单击下拉菜单时,控制台日志中没有 jquery 错误。 Here's my applicaiton.js:这是我的 applicaiton.js:

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

I have the JQuery-Rails gem along with jquery-ui-rails.我有 JQuery-Rails gem 和 jquery-ui-rails。 The console log shows now errors with jquery/javascript when I click on the dropdown either.当我单击下拉列表时,控制台日志现在显示 jquery/javascript 错误。 Here's my server logs:这是我的服务器日志:

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)

Here's the view erb file for the navbar component.这是导航栏组件的视图 erb 文件。 It was basically taken directly off the bootstrap website:它基本上是直接从引导程序网站上删除的:

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

Setting an explicit href ( "#" ) will make your browser change the url fragment, you can remove it ( <a href class="..." ...> ) and the browser wont try to follow it.设置显式 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>

On the other side, the drop down should toggle.另一方面,下拉菜单应该切换。 Check the network inspector and ensure you are actually loading the application JavaScript at all.检查网络检查器并确保您实际上正在加载应用程序 JavaScript。

UPDATE更新

There is an error in your application.js file.您的application.js文件中存在错误。 The libraries are just named and not require d.这些库只是命名的, require d。 It should read:它应该是:

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

(also notice the dot at the end of require_tree line) (还要注意require_tree行末尾的点)

I found the answer here我在这里找到了答案

Had to add the following script tags under my closing body tag, in my application.html.erb layout file:必须在我的 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>

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

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