【问题标题】:Rails with Bootstrap (on Windows) - empty application.css file and styling won't load带有 Bootstrap 的 Rails(在 Windows 上)- 空的 application.css 文件和样式不会加载
【发布时间】:2017-03-08 09:05:38
【问题描述】:



GemFile 相关部分:


捆绑安装:


仍然没有效果:


来自 \app\views\layouts\application.html.erb:

<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>

application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require turbolinks
//= require_tree .

如何加载 Bootstrap 样式?

【问题讨论】:

  • 我可以知道您的 app/assets/stylesheets 文件夹中的所有文件吗?

标签: ruby-on-rails windows ruby-on-rails-5 twitter-bootstrap-rails


【解决方案1】:

您似乎缺少导入引导样式。要根据bootstrap-sass documentation 解决此问题,您应该:

app/assets/stylesheets/application.scss中导入引导样式

// "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables"
@import "bootstrap-sprockets";
@import "bootstrap";

bootstrap-sprockets 必须在 bootstrap 之前导入,图标字体才能工作。

确保文件具有 .scss 扩展名(或 .sass 用于 Sass 语法)。如果你刚刚生成了一个新的 Rails 应用程序,它可能会附带一个 .css 文件

【讨论】:

    【解决方案2】:

    首先,您应该决定使用哪个 gem,twitter-bootstrap-railsbootstrap-sass,而不是两者。

    假设你使用bootstrap-sass 那么你的application.js 应该是

    //= require bootstrap-sprockets

    不是

    //= require twitter/bootstrap

    然后添加

    @import "bootstrap-sprockets";
    @import "bootstrap";
    

    给你的app/assets/stylesheets/application.scss 就像R. Sierra 说的一样。

    现在您的应用应该可以运行了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      相关资源
      最近更新 更多