views:

210

answers:

2

What are some good methods for handling CSS in large Rails projects? Ideally I'd like to be able to include CSS or a link to a CSS file per partial.

I've played around with using content_for and yields which allows me to insert CSS into the head of the page with partials that are located elsewhere, however some partials get used more than once which would result in a double up of style links.

My ideal solution would be the ability to have a stylesheet link tag per partial that is inserted into the head, then in production these links will be collated into one big stylesheet that is only included once.

+4  A: 

You should consider looking into Sass and Compass. Sass gives you a brilliant way to generate CSS. Compass gives you a framework to manage all your Sass stylesheets and mixins more easily.

Sass makes CSS fun again.

Compass is a stylesheet authoring framework that makes your stylesheets and markup easier to build and maintain. With compass, you write your stylesheets in Sass instead of CSS.

Ryan McGeary
+1. Use Sass (and its sister haml for the HTML instead of erb!)
wesgarrison
I've found HAML to be a solution in search of a problem. SASS seems useful though however Compass really seems like overkill.I'd like a solution that's somewhere in-between. 1 stylesheet for the whole project, then one stylesheet per partial which is not double included if the partial is used more than once. Then once the environment is set to production, all stylesheets are combined into one.
Samuel
Give both Haml and Compass another chance. They're indispensable, IMO.
Ryan McGeary
A: 

Compass is a great library, but I prefer much more minimal solutions. Like Samuel, mentioned, it is overkill. But, I do think Sass (especially 3.0) is worth getting into. Mixins, variables, functions are all things CSS should have :)

I deploy to Heroku, which makes compiling stylesheets to disk tricky. So I wrote a simple workaround which I describe here:

http://avandamiri.com/2010/09/15/managing-styles-with-sass-on-heroku.html

The trick is to have the server compile them on request and then cache result with Varnish. I hope it helps.

Avand Amiri