简体   繁体   English

为什么我的Rails应用程序上的Bootstrap导航栏是垂直的而不是水平的?

[英]Why is the Bootstrap navbar on my Rails app vertical instead of horizontal?

I have a rails app and the navbar made on bootstrap should be horizontal but it is vertical. 我有一个Rails应用,引导程序上创建的导航栏应该是水平的,但它是垂直的。 here is the link to the app https://shielded-wave-99449.herokuapp.com/ I have checked other articles on here (see below) but they did not solve the issue. 这是应用程序的链接https://shielded-wave-99449.herokuapp.com/我已经在这里查看了其他文章(请参阅下文),但他们没有解决问题。

bootstrap navbar displays vertically instead of horizontally 引导导航栏显示为垂直而不是水平

Bootstrap navbar appearing vertically instead of horizontal Bootstrap导航栏显示为垂直而不是水平

Bootstrap navbar is displaying vertically instead of horizontally Bootstrap导航栏显示为垂直而不是水平

navbar is vertical instead of horizontal 导航栏是垂直的,而不是水平的

I am following a tutorial and have the same code as him and his navbar is working. 我正在学习教程,并且代码与他相同,并且他的导航栏正在运行。

Here is the link to the GitHub repository for the tutorial for reference: https://github.com/StephenFiser/FrogBlog/blob/master/app/views/layouts/_navbar.html.erb 这是本教程的GitHub存储库的链接以供参考: https : //github.com/StephenFiser/FrogBlog/blob/master/app/views/layouts/_navbar.html.erb

The code for the navbar is in a partial and there are 2 one drop down and one normal. 导航栏的代码是局部的,有2个下拉菜单和1个正常菜单。 they are both displaying vertically. 它们都垂直显示。

Here is my navbar partial _navbar.html.erb 这是我的导航栏局部_navbar.html.erb

#----------dropdown navbar menu code----------

<div class="collapse" id="exCollapsingNavbar">
  <div class="collapse-bg p-a-1">
    <div class='card'>
      <ul class="list-group list-group-flush">
        <li class="list-group-item">
          <%= link_to 'Blog', root_path, class: "nav-link #{yield(:blog_active)}" %>
        </li>
        <li class="list-group-item">
          <%= link_to 'About', about_path, class: "nav-link #{yield(:about_active)}" %>
        </li>
        <li class="list-group-item">
          <%= link_to 'Contact', contact_path, class: "nav-link #{yield(:contact_active)}" %>
        </li>
        <% if author_signed_in? %>
          <li class="list-group-item">
            <%= link_to 'My posts', authors_posts_path, class: "nav-link #{yield(:author)}" %>
          </li>
          <li class="list-group-item">
            <%= link_to 'Logout', destroy_author_session_path, method: :delete, class: "nav-link" %>
          </li>
        <% end %>
      </ul>
    </div>
  </div>
</div>


#---------------normal non-dropdown navbar code-----------

<nav class="navbar navbar-light bg-faded">
  <div class='container'>
    <a class='navbar-brand' href='/'>
      Frog<span class='light'>blog</span>
    </a>
    <button class="navbar-toggler hidden-sm-up pull-xs-right" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar">
      &#9776;
    </button>
    <ul class="nav navbar-nav pull-sm-right hidden-xs-down">
      <li class="nav-item">
        <%= link_to 'Blog', root_path, class: "nav-link #{yield(:blog_active)}" %>
      </li>
      <li class="nav-item">
        <%= link_to 'About', about_path, class: "nav-link #{yield(:about_active)}" %>
      </li>
      <li class="nav-item">
        <%= link_to 'Contact', contact_path, class: "nav-link #{yield(:contact_active)}" %>
      </li>
      <% if author_signed_in? %>
        <li class="nav-item dropdown author-nav">
          <a class="nav-link dropdown-toggle <%= yield(:author) %>" href="#" id="navbarDropdownMenuLink"
             data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
             <%= image_tag(current_author.gravatar_image_url) %>
             <%= current_author.display_name %>
          </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <%= link_to 'My posts', authors_posts_path, class: "dropdown-item" %>
            <%= link_to 'Account', authors_account_path, class: "dropdown-item" %>
            <%= link_to 'Logout', destroy_author_session_path, method: :delete, class: "dropdown-item" %>
          </div>
        </li>
      <% end %>
    </ul>
  </div>
</nav>

It seems that the container for the navbar is too high and that this needs adjusting. 导航栏的容器似乎过高,需要对此进行调整。 So should I try to change the container? 那么我应该尝试更换容器吗? (if so how do I find this css on rails?) or is there another bootstrap class I can add to make the navbar vertical? (如果是这样,如何在导轨上找到这个CSS?)还是可以添加另一个引导类以使导航栏垂直?

If you are using Bootstrap 4, then you should use the components it provides. 如果使用的是Bootstrap 4,则应使用其提供的组件。 Right now you seem to be trying to re-invent the wheel and that's the main reason for your struggle. 现在,您似乎正在尝试重新发明轮子,这是您奋斗的主要原因。 You are simply not using the Bootstrap classes and components. 您只是不使用Bootstrap类和组件。 That's why you are not getting the expected outcome. 这就是为什么您没有得到预期的结果。

Here's a working code snippet for a Bootstrap 4 navbar: 这是Bootstrap 4导航栏的工作代码段:

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <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> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Dropdown </a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="#">Something else here</a> </div> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul> <form class="form-inline my-2 my-lg-0"> <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> </form> </div> </nav> 

So, your Rails code needs to produce that HTML output for that sample navbar to work. 因此,您的Rails代码需要产生该HTML输出,以便该示例导航栏起作用。

First, adjust that HTML template to produce the result you want and then adjust your Rails code to produce that HTML. 首先,调整该HTML模板以产生所需的结果,然后调整Rails代码以产生该HTML。

Reference: 参考:

https://getbootstrap.com/docs/4.0/components/navbar/ https://getbootstrap.com/docs/4.0/components/navbar/

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

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