如何让Rails 3.1默认使用SASS(Over SCSS)?

发布于 2024-11-07 07:11:10 字数 372 浏览 0 评论 0原文

很难弄清楚如何使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

装纯掩盖桑 2024-11-14 07:11:10

设置 config:

config.sass.preferred_syntax = :sass

对于rails 3.1.rc4,您可以在 application.rb 文件中

For rails 3.1.rc4, you could set the config:

config.sass.preferred_syntax = :sass

in the application.rb file

纸短情长 2024-11-14 07:11:10

我将以下内容添加到 config/environments/development.rb 中:

config.sass.preferred_syntax = :sass

这就成功了。

I added the following to config/environments/development.rb:

config.sass.preferred_syntax = :sass

That did the trick.

羁客 2024-11-14 07:11:10

执行 require 'sass/plugin' 并确保它在您的 Application.initialize! 调用之后位于底部。

Do require 'sass/plugin' and make sure it's at the bottom after your Application.initialize! call.

你另情深 2024-11-14 07:11:10

与 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.rb

I 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?

我很坚强 2024-11-14 07:11:10

正如 @krainboltgreene 所评论的,将以下行添加到 config/application.rb

config.generators.stylesheet_engine = :sass

使 sass 成为样式表生成器的默认格式。然而,由于 Rails 3.1.beta1 不支持它,因此会收到以下错误消息

$ rails g scaffold user name:string
...
Could not find "scaffold.css.sass" in any of your source paths. Your current source paths are:
.../gems/railties-3.1.0.beta1/lib/rails/generators/rails/scaffold/templates
...

$ rails g controller users
...
Could not find "stylesheet.css.sass" in any of your source paths. Your current source paths are: 
.../gems/railties-3.1.0.beta1/lib/rails/generators/rails/assets/templates

如您所见,无法在不破坏生成器的情况下更改默认格式。相反,您可以手动创建额外的 *.css.sass 文件,无论有没有 scss 文件,这些文件都可以正常工作。

As @krainboltgreene commented, adding the following line to config/application.rb

config.generators.stylesheet_engine = :sass

makes sass the default format for stylesheet generators. However, since Rails 3.1.beta1 doesn't support it, one gets the following error messages

$ rails g scaffold user name:string
...
Could not find "scaffold.css.sass" in any of your source paths. Your current source paths are:
.../gems/railties-3.1.0.beta1/lib/rails/generators/rails/scaffold/templates
...

$ rails g controller users
...
Could not find "stylesheet.css.sass" in any of your source paths. Your current source paths are: 
.../gems/railties-3.1.0.beta1/lib/rails/generators/rails/assets/templates

As 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.

别再吹冷风 2024-11-14 07:11:10

我在其他地方找到了这个答案,记不清了,但把它放在 config/initializers/sass.rb 中:

Sass::Engine::DEFAULT_OPTIONS[:load_paths].tap do |load_paths|
  load_paths << "#{Rails.root}/app/assets/stylesheets"
  load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets"
end

我也更喜欢 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:

Sass::Engine::DEFAULT_OPTIONS[:load_paths].tap do |load_paths|
  load_paths << "#{Rails.root}/app/assets/stylesheets"
  load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets"
end

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 your application.css to application.css.sass, change the comments at the top to // instead of /* */ and use the require_* directives—it all works, and then you can use SASS in your application global stylesheet. It won't if you use compass in app/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文