简体   繁体   中英

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

bootstrap navbar displays vertically instead of horizontally

Bootstrap navbar appearing vertically instead of horizontal

Bootstrap navbar is displaying vertically instead of horizontally

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

The code for the navbar is in a partial and there are 2 one drop down and one normal. they are both displaying vertically.

Here is my navbar partial _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?

If you are using Bootstrap 4, then you should use the components it provides. 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. That's why you are not getting the expected outcome.

Here's a working code snippet for a Bootstrap 4 navbar:

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

First, adjust that HTML template to produce the result you want and then adjust your Rails code to produce that HTML.

Reference:

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

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