60

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)
krainboltgreene
  • 1,841
  • 2
  • 17
  • 25

6 Answers6

74

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

config.sass.preferred_syntax = :sass

in the application.rb file

Kevin
  • 1,845
  • 13
  • 12
  • 7
    If you do this, it won't deploy in production b/c the sass and :asset group isn't active for production environments. You'll get a sass method not found being thrown by your deploy script. Personally, I ended out having to work around that by changing my Gemfile, commenting out the "group :assets" part, so that sass-rails, coffee-rails, uglifier gems were loaded for all configurations, not just as part of the assets group, like so: # group :assets do gem 'sass-rails', " ~> 3.1.0" gem 'coffee-rails', " ~> 3.1.0" gem 'uglifier' # end – Rob Sep 23 '11 at 11:09
  • I made this change but it's still using scss as the default according to `rails g scaffold --help`. Any ideas? – Explosion Pills Jul 18 '12 at 01:56
  • 4
    Can't you just `rake assets:precompile` before pushing to production? – Seed Dec 18 '13 at 16:30
8

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

config.sass.preferred_syntax = :sass

That did the trick.

remino
  • 324
  • 2
  • 17
7

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

aceofspades
  • 7,568
  • 1
  • 35
  • 48
  • 1
    Just figured this out, like 5 minutes ago. The other solution is to have that require in a `config/initializers/sass.rb` file. – krainboltgreene May 15 '11 at 06:02
  • Looking further into it, it seems that still doesn't set SASS as the default for stylesheet generation. – krainboltgreene May 15 '11 at 06:12
  • 8
    Ok, the real solution is to have this in your `application.rb` file: `config.generators.stylesheet_engine = :sass` – krainboltgreene May 15 '11 at 06:19
  • 2
    While this fixes the issue of the error being thrown, I still can't seem to set the 'style' option so that it carries through to the generated stylesheet - The style line in the question worked for me in 3.0.7, but not now in 3.1. Any ideas? – sevenseacat May 21 '11 at 10:58
2

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?

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
2

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.

Andrei
  • 10,918
  • 12
  • 76
  • 110
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.

sj26
  • 6,725
  • 2
  • 27
  • 24