如何在Rails 3.1中使用指南针(Compass)?

35

我搜索了很多,但是我只看到使用rails 3.1与compass结合的方法就是编辑Gemfile文件,例如:

gem 'compass', :git => 'https://github.com/chriseppstein/compass.git', :branch => 'rails31'
gem 'sass-rails', "~> 3.1.0.rc"

是的,我理解了那个方法,但接下来怎么做呢?我看到的所有教程都只是说使用某个特定的分支。但我还是在使用Rails 3.1时遇到了使用Compass的问题。

我已经尝试了以下方法:

$ compass init rails . --syntax sass
  directory ./app/stylesheets/ 
  create ./config/compass.rb 
  create ./app/stylesheets/screen.sass 
  create ./app/stylesheets/print.sass 
  create ./app/stylesheets/ie.sass

由于3.1版本现在使用资源文件,我将所有这些文件转移到了3.1版本。此外,我正在使用compass-960插件,那么我应该在哪里引用它?我尝试添加一个compass.rb文件并添加require 960和require html5-boilerplate,但我仍然不断收到错误信息:

Error compiling asset application.css:
NoMethodError: undefined method `Error' for Compass:Module
  (in /Users/eumir/rails_apps/kiseki/app/assets/stylesheets/screen.sass)

NoMethodError (undefined method `Error' for Compass:Module
(in /Users/eumir/rails_apps/kiseki/app/assets/stylesheets/screen.sass)):

我尝试运行 compass compile,但是出现了以下提示:

$ compass compile Nothing to compile. If you're trying to start a new project, you have left off the directory argument. Run "compass -h" to get help.

就像我之前说的那样,我已经编辑了我的 compass.rb 文件,所以现在还不知道该怎么处理。有人能提供帮助吗?


我不确定,但我认为在没有大量黑客攻击的情况下,它还不能在3.1上运行。 - noli
今天我成功升级了我的应用程序(它使用指南针)- 我在这里记录了它:http://webtempest.com/upgrade-rails-3-to-3-1/ - Plattsy
4个回答

33
更新: 看起来有更好的方法!
来源: http://spin.atomicobject.com/2011/07/12/sass-sprockets-compass-with-rails-3-1/ 更新2 (2011年12月2日):Compass的创建者Chris Eppstein发布了这个Github Gist,介绍如何将Compass与Rails 3.1集成: https://gist.github.com/1184843

现在我更喜欢这种方法,因为我注意到使用livereload编译时速度得到了很大提升。


我的方法:
(我现在认为它已过时,但是也许在某些情况下它仍然有用,因此我在此提供参考:)

首先,在您的Gemfile中添加:

gem "compass", "~> 0.12.alpha.0"

也不要忘记执行一个

bundle update

然后,在 config/application.rb 中:

config.generators.stylesheet_engine = :sass
将application.css.scss重命名为application.css.sass,或者创建一个名为application.css.sass的文件,并将其内容替换为:
@import compass
@import _blueprint

如果你想让新的Rails 3.1清单代码保留在样式表的开头,你需要将每行开头的'/* */'注释替换为Sass语法版本的'//'。

现在,为了测试compass和blueprint mixins是否起作用,在同一文件application.css.sass中添加一些代码:

@import compass
@import _blueprint
body
  background: black
  +linear-gradient(color-stops(white, black))
  +column(5)

使用以下命令启动Rails服务器:

bundle exec rails server

在浏览器中加载您的应用程序, 并访问 http://localhost:3000/assets/application.css

如果一切顺利,您应该看到已编译的代码。

来源:

http://blog.pixarea.com/2011/07/using-compass-blueprint-semantic-and-sass-syntax-in-rails-3-1/


1
将我的答案更新为使用Compass gem版本“0.12.alpha.0”,而不是分支“rails31”。 - Daniel R
更新了我的回答:有一种新的更好的方法,请查看提供的链接。 - Daniel R
有一个更好的方法,就像Chris Eppstein从Compass现在发布了如何将Compass集成到3.1中的方法:https://gist.github.com/1184843 - corroded
希望你不介意我更新你的答案 :) 我添加了链接到克里斯的代码片段。 - corroded

6

其他答案中的解决方案已经过时,最新版本的Compass v0.12需要适配器才能与rails应用程序一起使用。Compass的作者Chris Eppstein在github上发布了安装说明:
https://github.com/compass/compass-rails

该适配器支持2.3及更高版本的rails。


我测试了一下,但似乎在Rails 3.1.0上有漏洞。 - corroded
1
是的,在我创建问题时,Scott已经确认了这一点。它适用于Rails 3.1.3,而不是3.1.0。我认为这是与Sprockets的一些遗留问题有关。 - corroded

1

Peter Gumeson来自罗盘用户组,向我指出了这个问题的解决方法:

https://groups.google.com/forum/#!msg/compass-users/mU5HBt8TCIg/2Rt7iSESTSAJ

这是他的留言:

Hi gang. This github issue might help out. https://github.com/sporkd/compass-html5-boilerplate/issues/19

I am pretty much running everything on edge right now. So my gemfile looks something like this:

gem 'rails',     :git => 'git://github.com/rails/rails.git'
gem 'sass-rails', '~> 3.1.0.rc2'
gem 'haml', :git => 'git://github.com/nex3/haml.git'
gem 'haml-rails'
gem "compass", :git => "git://github.com/chriseppstein/compass.git", :tag => "0.12.alpha.0"
gem 'compass-html5', :git => 'git://github.com/sporkd/compass-html5.git'

I'm working on the rails generators right now, so it should not be too far off. But this should at least get you going.

Peter

*按照choonkeat的話更改分支


1
分支“rails31”已经消失。暂时试试这个:“gem”compass“,:git => ”git://github.com/chriseppstein/compass.git“, :tag => ”0.12.alpha.0“”。 - choonkeat
:tag => "0.12.alpha.0" 对我没用,但是 :tag => "v0.12.alpha.0" 有效。 - kulpae

0

您可以下载Compass目录,将其放入vendor/assets/stylesheets中,以便您的目录结构为vendor/assets/stylesheets/compass。然后在您的主应用程序样式表中按需包含Compass混合器:@include compass/reset;


无法在Rails 3.1 rc6中使用,尝试从app/assets/stylesheets下的application.scss导入。 - Aldo

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