在Rails 3应用程序中出现了Sass导入错误 - "无法找到或读取要导入的文件:compass"

24

我的Rails 3应用程序可以成功运行compass init rails ./ --using blueprint。我可以从/stylesheets目录中@import文件,但是当我尝试@import compass时会出现错误。

现在该应用程序有两个简单的sass文件:

common.scss

$body-background: blue;

main.scss

@import "common";
//@import "compass";

body { 
  background: $body-background; 
}

如果注释掉 @import "compass" 这行,那么这段代码是可以工作的——我会得到一个蓝色的背景,因此我知道 Sass 是正常工作的。

但是如果我取消注释这行,并尝试导入 compass 或者 blueprint 或其他任何东西,我会得到以下类似的错误信息。

Syntax error: File to import not found or unreadable: compass.
              Load paths:

                /Users/eric/path/to/myrailsapp/app/stylesheets
        on line 2 of /Users/eric/path/to/myrailsapp/app/stylesheets/main.scss

1: @import "common";
2: @import "compass";
3: 
4: body { 
5:  background: $body-background; 
6: }

我曾认为可能需要明确告诉Sass去哪里找Compass gem,因此我在config/compass.rb中添加了一行add_import_path

require 'compass'
require 'html5-boilerplate'

project_type = :rails
project_path = RAILS_ROOT if defined?(RAILS_ROOT)
add_import_path "/Library/Ruby/Gems/1.8/gems/"  # unfortunately this has no effect

http_path = "/"
css_dir = "public/stylesheets"
sass_dir = "app/stylesheets"
images_dir = "public/images"
javascripts_dir = "public/javascripts"
cache_dir = "tmp/sass-cache"

http_images_path = "/images"
http_stylesheets_path = "/stylesheets"
http_javascripts_path = "/javascripts"

我已经谷歌了两天,但是无法确定为什么使用基本的@import语句时会出现问题。如何告诉Sass去哪里找Compass和Blueprint库?

7个回答

21

我遇到了这个错误。

我在我的application.rb中将这行代码更改为:

Bundler.require(:default, Rails.env) if defined?(Bundler)

至:

Bundler.require(*Rails.groups(:assets => %w(development test))) if defined?(Bundler)

此外,请确保文件名为something.css.sass而不是something.sass

还有一件事,我在我的配置目录中有一个旧的compass.rb文件,在Rails 3.2中不需要。删除它也为我解决了这个问题。


今天我创建了一个新的Rails 3.2.8项目,但是compass.rb出了问题。我没有在application.rb中做任何更改。所以首先删除compass.rb - Ashley

9
我通过在我的Gemfile中使用 compass-rails gem而不是 compass gem来解决了这个问题(别忘了重新启动你的服务器)。以防万一有人会遇到同样的问题。

5

在我苦思冥想后,我解决了这个错误。

我在我的application.rb文件中注释掉了这一行:

Bundler.require(*Rails.groups(:assets => %w(development test))) if defined?(Bundler)

并去掉注释:

Bundler.require(:default, :assets, Rails.env) if defined?(Bundler)    

我正在使用Rails 3.2.11


如果您已经添加了一个新的环境,将其添加到%w(development test)中,例如%w(development test new_env_name)就足以使资产加载。 - spyle

4

好的,在得到了出色的Chris Eppstein的帮助后,我找到了有问题的行。在config/environments.rb中,我有这样一行:

Sass::Plugin.options[:template_location] = {
"#{RAILS_ROOT}/app/stylesheets" => "#{RAILS_ROOT}/public/stylesheets"
}

在当前版本的Rails(我使用的是3.0.7)和Compass(0.11.1)中,这行代码不再必要。我在跟随教程后添加了它。如果sass找不到compass,可能是因为这行代码破坏了你的Sass::Plugin.options[:template_location]


3

在Rails 3.2.13和compass-rails 1.0.3上,这对我没有起作用。 - mcmlxxxiii

2

@import命令在Sass中导入.scss或.sass文件,而不是.css文件。

看起来安装过程中有些东西被忽略了(也许是缺少了compass install blueprint这行代码?)。我按照这里的步骤进行操作:http://compass-style.org/reference/blueprint/,但无法重现此问题。


没错,我正在尝试包含常规的指南针和蓝图(scss)文件。上面的“compass init”行安装了蓝图。为了确认,我再次运行了该命令--您可以在下面的输出中看到compass认为一切都已安装完毕。 - Eric Wright
$ compass install blueprint app/stylesheets/screen.scss 已存在,未更改 app/stylesheets/partials/_base.scss 已存在,未更改 创建 app/stylesheets/print.scss 创建 app/stylesheets/ie.scss public/images/grid.png 已存在,未更改 - Eric Wright

0
我这样修复了这个错误:
在 application.rb 文件中,我有以下内容:
Bundler.require(:default, Rails.env) if defined?(Bundler)  

并且被改为

Bundler.require(:default, :assets, Rails.env) if defined?(Bundler)

这是在将项目从3.0.3更新到3.2.13之后发生的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接