Rails 4 Bootstrap不加载

时间:2015-12-09 03:45:12

标签: ruby-on-rails twitter-bootstrap

我真的处于一个突破点,所以我希望有人能在这里帮助我。

我已按照此文档(以及8个教程)https://github.com/twbs/bootstrap-rubygem#a-ruby-on-rails

执行此操作并尝试将getbootstrap网站上的组件/示例页面中的导航栏添加到我的appliaction.html.erb文件后,我确实在页面上看到文本加载但我从未将任何CSS应用于该页面。

它根本就没有用,而且我最终想要弄清楚。

更新

我有https://github.com/KR0SIV/learning-bootstrap

Application.css.scss

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require_tree .
 *= require_self
 */
@import "bootstrap-sprockets";
@import "bootstrap";

然后只是看看它是否可行我只是将示例中的导航栏添加到我的项目application.html.erb文件

<!DOCTYPE html>
<html>
<head>
  <title>LearningBootstrap</title>

</head>
<body>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>


<%= yield %>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

首先。您的项目中有多个应用程序样式表。 application.css application.css.scss 。删除 application.css 并将另一个重命名为 application.scss

删除以下行:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require_tree .
 *= require_self
 */

Bootstrap gem README写道:

  

然后,从中删除所有* = require和* = require_tree语句   Sass文件。相反,使用@import导入Sass文件。

     

不要在Sass中使用* = require,否则您的其他样式表不会   能够访问Bootstrap mixins和变量。

这可能是你的问题,因为你说它没有找到一个sass变量。

要导入所有其他CSS文件,请添加以下行以打开引导程序导入:

@import '**/*';

这将递归添加所有CSS文件。

当然,您必须将样式表添加到HTML模板中。

只需使用Rails中的默认值:

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>

重要! Bootstrap 4仍在开发中,目前只是一个Alpha版本。我建议您将Bootstrap 3用于生产应用。但如果它只是为了学习它就完全没问题。

如果有官方宝石,我建议您不要使用像twitter-bootstrap-rails这样的第三方宝石。

相关问题