views:

1074

answers:

5

If you are using HAML and SASS in your Rails application, then any templates you define in public/stylesheet/*.sass will be compiled into *.css stylesheets. From your code, you use stylesheet_link_tag to pull in the asset by name without having to worry about the extension.

Many people dislike storing generated code or compiled code in version control, and it also stands to reason that the public/ directory shouldn't contain elements that you don't send to the browser.

What is the best pattern to follow when laying out SASS resources in your Rails project?

A: 

If I can manage it, I like to store all of my styles in SASS templates when I choose HAML/SASS for a project, and I'll remove application.css and scaffold.css. Then I will put SASS in public/stylesheets/sass, and add /public/stylesheets/*.css to .gitignore.

If I have to work with a combination of SASS and CSS based assets, it's a little more complicated. The simplest way of handling this is to have an output subdirectory for generated CSS within the stylesheets directory, then exclude that subdirectory in .gitignore. Then, in your views you have to know which styling type you're using (SASS or CSS) by virtue of having to select the public/stylesheets/foo stylesheet or the public/stylesheets/sass-out/foo stylesheet.

If you have to go the second route, build a helper to abstract away the sass-out subdirectory.

Pete
+9  A: 

I always version all stylesheets in "public/stylesheets/sass/*.sass" and set up an exclude filter for compiled ones:

/public/stylesheets/*.css
mislav
+4  A: 

Honestly, I like having my compiled SASS stylesheets in version control. They're small, only change when your .sass files change, and having them deploy with the rest of your app means the SASS compiler doesn't ever need to fire in production.

The other advantage (albeit a small one) is that if you're not using page caching, your rails process doesn't need to have write access to your public_html directory. So there's one fewer way an exploit of your server can be evil.

Nate
+5  A: 

Somewhat related, but it's a good idea to regenerate your CSS during your capistrano deployments. This callback hook does just that:

after "deploy:update_code" do
  rails_env = fetch(:rails_env, "production")
  run "#{release_path}/script/runner -e #{rails_env} 'Sass::Plugin.update_stylesheets'"
end

Update: This should no longer be necessary with modern versions of Haml/Sass.

Ryan McGeary
+9  A: 

The compass framework recommends putting your sass stylesheets under app/stylesheets and your compiled css in public/stylesheets/compiled.

You can configure this by adding the following code to your environment.rb:

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

If you use the compass framework, it sets up this configuration for you when you install it.

chriseppstein