Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top