【问题标题】:Bootstrap Navbar Images not appearing in Rails引导导航栏图像未出现在 Rails 中
【发布时间】:2018-05-08 20:38:08
【问题描述】:

我无法显示应用程序中的文件图像。现在它的位置只是一个图标,表明它们应该是一个图像。我现在正在专门研究徽标(导航栏品牌)。现在图像在同一个文件夹中。

如何正确显示图形并调整其大小?

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width= width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
    /* Remove the navbar's default margin-bottom and rounded borders */
    .navbar {
      margin-bottom: 0;
      border-radius: 0;
    }

    /* Add a gray background color and some padding to the footer */
    footer {
      background-color: #f2f2f2;
      padding: 25px;
    }

  .carousel-inner img {
      width: 100%; /* Set width to 100% */
      margin: auto;
      min-height:200px;
  }

  .navbar-brand {
  width: 57px;
  height: 27px;
  }
  .navbar-brand img {
  width: 30px;
  height: 30px;
  }

  /* Hide the carousel text when the screen is less than 600 pixels wide */
  @media (max-width: 600px) {
    .carousel-caption {
      display: none;
    }
  }
  </style>
</head>
<body>

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>

      <div class="pull-left">
          <img src="rsz_1logo" class="navbar-brand">
      </div>

    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><%= link_to "Home", root_path %></li>
        <li><%= link_to 'Learn More', controller: 'posts' %> </li>
        <% if user_signed_in? %>
        <li><%= link_to 'Calculate Water Footprint', controller: 'waterusages' %></li>
        <li><%= link_to 'See Your Usage', waterusage_result_path %></li>
        <% end %>

        <!--  <a href="/">Calculate Water Footprint</a></li> -->
        <!-- <li><a href="#">Projects</a></li> -->
        <li><a href="#">About Us</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <% if !user_signed_in? %>
        <li> <%= link_to('Sign Up', new_user_registration_path)  %> </li>
        <li> <%= link_to('Login', new_user_session_path)  %> <span class="glyphicon glyphicon-log-in"></span> </li>
        <% end %>

        <% if user_signed_in? %>
        <li> <%= link_to('Logout', destroy_user_session_path, method: :delete) %> </li>
        <% end %>

      </ul>
    </div>
  </div>
</nav>

</body>
</html>

【问题讨论】:

  • src="rsz_1logo" 不是合适的来源。同样在 Rails 中,您必须将图像存储在 app/assets/images 目录中。在您的 HTML 中,将 &lt;img&gt; 替换为 &lt;%= image_tag "your_image.jpg", alt="Alt text" %&gt;

标签: ruby-on-rails twitter-bootstrap image navbar


【解决方案1】:

两件事。

首先,您应该使用 image_tag 助手在您的 Html 中生成图像元素,而不是硬编码 src 属性。

其次,您需要将图像放入资产目录。

Rails 指南应该会有所帮助。

http://guides.rubyonrails.org/asset_pipeline.html#asset-organization

【讨论】:

  • image_tag 和指南确实有助于显示图像,但我仍然没有看到如何调整它的大小。
  • 要调整它的大小,请将课程交给您的&lt;%= image_tag %&gt;。这是&lt;%= image_tag "your_image.jpg", alt="Alt text", class: "logo" %&gt;。现在你可以使用 css .logo { }
猜你喜欢
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2016-05-20
  • 2015-05-29
相关资源
最近更新 更多