简体   繁体   English

Bootstrap导航栏和Rails应用程序中的按钮

[英]bootstrap navbar and button in rails app

I have a boostrap navbar integrated in my rails app. 我的rails应用程序中集成了boostrap navbar。 It is made of: 它是由:

  • my logo and some links to the left 我的徽标和左侧的一些链接
  • a "New post" button to the right. 右侧的“新帖子”按钮。

First, here is my code: 首先,这是我的代码:

  <div class="container">
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <div class="pull-left" id="navbar-logo">
            <%= image_tag 'physics.png', width: "32", alt: 'logo' %>
          </div>
          <a class="navbar-brand">Podcasts</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul id="navbar" class='nav navbar-nav'>
            <li class="<%= 'active' if current_page?(home_path) %>">
              <%= link_to 'Home', home_path %></li>
            <li><%= link_to 'Archives', '#' %></li>
            <li class="<%= 'active' if current_page?(about_path) %>">
              <%= link_to 'About', about_path %></li>
            <li class="<%= 'active' if current_page?(contact_path) %>">
              <%= link_to 'Contact', contact_path %></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <div class="btn-group">
              <%= button_to "New Post", "#", class: 'btn btn-primary navbar-btn'%>
            </div>
          </ul>
      </div>
  </nav>
</div>

Here are my questions: 这是我的问题:

1- I noticed that, even if <nav class="navbar navbar-default navbar-fixed-top"> is nested in <div class="container"> , it takes the whole width of my screen. 1-我注意到,即使<nav class="navbar navbar-default navbar-fixed-top">嵌套在<div class="container"> ,它也会占用整个屏幕的宽度。 But when I replace it by <nav class="navbar navbar-default"> , it is bounded by the container standard width. 但是,当我用<nav class="navbar navbar-default">替换它时,它受容器标准宽度的限制。 What's happening and how can I made this navbar-fixed-top bounded by the container? 发生了什么事,如何使此navbar-fixed-top受到容器的限制?

2- I want some nice icon next to my button "New Post" but I cannot figure how to add the bootstrap glyphicon glyphicon-plus whithin the button since the class is contained in the rails code : <%= button_to "New Post", "#", class: 'btn btn-primary navbar-btn'%> 2-我想在按钮“ New Post”旁边添加一个漂亮的图标,但是由于该类包含在rails代码中,因此我无法弄清楚如何在按钮中添加引导glyphicon glyphicon-plus<%= button_to "New Post", "#", class: 'btn btn-primary navbar-btn'%>

For the second part of the question use content_tag 对于问题的第二部分,请使用content_tag

<%= button_to "#", class: 'btn btn-primary navbar-btn' do %>
  New Post
  <%= content_tag(:span, "", :class => "glyphicon glyphicon-plus") %>
<% end %>

The .navbar-fixed-top adds a few styles to the default. .navbar-fixed-top为默认样式添加了一些样式。 And that makes it take the full width. 这使得它占据了整个宽度。 If you want it bounded by .container you need to add this styles to .navbar-fixed-top class. 如果您希望它受.container限制, .container需要将此样式添加到.navbar-fixed-top类。

.navar-fixed-top{
    right: auto;
    left: auto;
    //You have to add border-radius of 3px if you want smooth edges.
}

Second, you can add as @codeVishal suggested 其次,您可以添加为@codeVishal建议

<%= content_tag(:span, "", :class => "glyphicon glyphicon-plus") %>

or you can add font-awesome icons. 或者您可以添加超棒的字体图标。 They are much neater. 他们更加整洁。 And its really simple to add. 而且添加起来真的很简单。 Add this to your head tag - 将此添加到您的头部标签-

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css">

and

<%= button_to "#", class: 'btn btn-primary navbar-btn' do %>
    <i class="fa fa-plus-circle"></i>&nbsp;New Post
<% end %>

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

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