【问题标题】:Ruby on Rails error when importing "bootstrap"导入“引导程序”时出现 Ruby on Rails 错误
【发布时间】:2014-06-27 13:15:47
【问题描述】:

我是 ruby​​ on rails 的新手,目前正在学习 Michael Hartl 的教程,不幸的是,当我尝试在 custom.css.scss 文件中调用 @import "bootstrap"; 时卡在了第五章。

我收到以下错误:

Sprockets::Rails::Helper::AssetFilteredError in StaticPages#home
  Showing /Users/name/Documents/Rails_projects/sample_app/app/views/layouts    /application.html.erb where line #5 raised:
Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( glyphicons-halflings.png )` to `config/initializers/assets.rb` and restart your server
      (in /Users/name/Documents/Rails_projects/sample_app/app/assets/styleshee
/custom.css.scss)
     Extracted source (around line #5):

line 5:    stylesheet_link_tag    "application", media: "all"

我已经在网上梳理了以下解决方案,但都没有奏效

  1. 上面为 assets.rb 文件提供的建议
  2. 使用 control+c 重启服务器
  3. 将 gem 文件“bootrap-sass”移动到 gem 文件中的部分 产品中不需要的资产
  4. 将 application.css 文件的扩展名更改为 应用程序.css.scss
  5. 直接调用@import “bootstrap”;在 application.css.scss 文件
  6. 在 config.ru 中添加以下内容:需要 'bootstrap-sass' #require bootstrap-sass 声明

这些都是我在网上找到的解决方案,但没有一个有效。

感谢任何帮助!

谢谢!

【问题讨论】:

  • 您要添加什么版本的引导程序?
  • 我正在使用 rails 4.1...
  • 我的答案也应该适用于 rails 4.1
  • Wali Ali - 非常感谢!这似乎现在可以工作了,但是我在 custom.css 中调用什么来获取引导,因为我不再使用 @import 了?
  • 不需要打电话。您创建一个扩展名为 .css 的文件。例如:custom.css(在 app/assets/stylesheets 内),然后您立即开始输入您的 css 代码。保存并刷新浏览器。也许也重新启动服务器(只是第一次)。它应该可以工作。

标签: css ruby-on-rails twitter-bootstrap


【解决方案1】:

不知道您是否能够解决问题,但这是我为使事情正常进行所做的。

错误

Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( glyphicons-halflings.png )` to `config/initializers/assets.rb` and restart your server

告诉你实际放

Rails.application.config.assets.precompile += %w( glyphicons-halflings.png )

在你的

config/initializers/assets.rb路径

在我这样做之后,我得到了另一个类似的错误,但这次要添加另一行。

Rails.application.config.assets.precompile += %w( glyphicons-halflings-white.png )

现在,您还需要确保在路径 app/assets/stylesheets/ 目录中的 application.css 文件中的 @import "bootstrap"; 行并将文件重命名为 application.css.scss

将这些放在config/initializers/assets.rb 文件中的Rails.application.config.assets.precompile 行之上然后重新启动服务器后,它实际上对我有用。希望对您有所帮助!

【讨论】:

    【解决方案2】:

    将 bootstrap 与 Rails 4.x 集成的另一种方法是:

    下载你想要的引导版本。解压缩文件并将其保存在本地计算机中。然后将文档移动到资产管道中的相关文件夹。像这样:

    将所有javascript文件(文件扩展名以.js结尾)移动到app/assets/javascripts

    将所有css文件(文件扩展名以.css结尾)移动到app/assets/stylesheets

    将所有图像移至app/assets/images

    app/assets/stylesheets 中,创建一个自定义文件custom.css。您可以使用该文件来覆盖引导程序功能。你不需要@import 方法。 您也不需要在 Gemfile 中添加 gem。只需下载您想要的引导程序版本并按照上面的说明进行操作。

    我相信教程使用bootstrap 2.3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 2015-10-28
      • 2016-09-24
      • 1970-01-01
      相关资源
      最近更新 更多