如何让Rails 3.1默认使用SASS(Over SCSS)?
很难弄清楚如何使 SASS 而不是 SCSS 作为样式表的默认值。
我尝试使用以下命令创建 sass_config.rb 文件:
Sass::Plugin.options[:syntax] = :sass
Sass::Plugin.options[:style] = :compressed
我还尝试将其添加到environment.rb 文件中。无论哪种方式我都会收到此错误:
.../config/environment.rb:7:in `<top (required)>':
uninitialized constant Sass::Plugin (NameError)
Having a hard time figuring out how to make SASS, not SCSS, as the default for stylesheets.
I've tried making a sass_config.rb
file with this:
Sass::Plugin.options[:syntax] = :sass
Sass::Plugin.options[:style] = :compressed
I've also tried adding that to the environment.rb file. Either way I get this error:
.../config/environment.rb:7:in `<top (required)>':
uninitialized constant Sass::Plugin (NameError)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
设置 config:
对于rails 3.1.rc4,您可以在
application.rb
文件中For rails 3.1.rc4, you could set the config:
in the
application.rb
file我将以下内容添加到 config/environments/development.rb 中:
这就成功了。
I added the following to
config/environments/development.rb
:That did the trick.
执行
require 'sass/plugin'
并确保它在您的Application.initialize!
调用之后位于底部。Do
require 'sass/plugin'
and make sure it's at the bottom after yourApplication.initialize!
call.与 scss 相比,我绝对更喜欢 sass - 你是否考虑过只使用 compass gem 来处理你的所有 CSS,并添加
preferred_syntax = :sass
到 config/compass.rb我还没有在 Rails 3.1 上测试过这个,但它在 3.0.7 中工作
排除
编辑作为故障 步骤,当您从 sass_config.rb 中删除第一行代码以便它只有第二行时会发生什么?这两行都会导致错误吗?
I definitely prefer sass to scss too - have you considered just using the compass gem for all your CSS, and adding
preferred_syntax = :sass
to config/compass.rbI haven't tested this out yet on rails 3.1 yet but it works in 3.0.7
EDIT
As a troubleshooting step, what happens when you remove just the first line of code from sass_config.rb so that it just has the second one? Do both these lines cause the error?
正如 @krainboltgreene 所评论的,将以下行添加到 config/application.rb
使 sass 成为样式表生成器的默认格式。然而,由于 Rails 3.1.beta1 不支持它,因此会收到以下错误消息
如您所见,无法在不破坏生成器的情况下更改默认格式。相反,您可以手动创建额外的 *.css.sass 文件,无论有没有 scss 文件,这些文件都可以正常工作。
As @krainboltgreene commented, adding the following line to
config/application.rb
makes
sass
the default format for stylesheet generators. However, since Rails 3.1.beta1 doesn't support it, one gets the following error messagesAs you see, one cannot change the default format without breaking the generators. Instead, you can manually create extra *.css.sass files, which are working fine with or without scss ones.
我在其他地方找到了这个答案,记不清了,但把它放在 config/initializers/sass.rb 中:
我也更喜欢 SASS 语法(相对于 SCSS)。您所要做的就是命名文件
mystylesheet.css.sass
,然后它就可以工作了。您甚至可以将application.css
重命名为application.css.sass
,将顶部的注释更改为//
而不是/* */
并使用require_*
指令 — 一切正常,然后您可以在应用程序全局样式表中使用 SASS。如果您在app/stylesheets
中使用指南针,则不会。不需要
Sass::Plugin
,它完全独立于基于 Sprockets 的新 Rails 资源引擎。它已经知道如何为您编译 SASS 并正确管理资产捆绑。我想新的 Compass 版本将使用资源管道自动为 Rails 3.1+ 项目执行此操作。
I found this answer somewhere else, can't remember exactly, but put this in
config/initializers/sass.rb
:I also prefer SASS syntax (to SCSS). All you have to do is name files
mystylesheet.css.sass
instead and it just works. You can even rename yourapplication.css
toapplication.css.sass
, change the comments at the top to//
instead of/* */
and use therequire_*
directives—it all works, and then you can use SASS in your application global stylesheet. It won't if you use compass inapp/stylesheets
.Don't require the
Sass::Plugin
, it's totally separate to the new Rails asset engine which is based on Sprockets. It already knows how to compile SASS for you and manages the bundling of assets properly.I imagine a new Compass release will do this automatically for Rails 3.1+ projects using the asset pipeline.