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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1. Use Sass (and its sister haml for the HTML instead of erb!)
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.
Give both Haml and Compass another chance. They're indispensable, IMO.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.