Skip to content

Instantly share code, notes, and snippets.

@fj
Created May 7, 2012 18:41
Show Gist options
  • Save fj/2629588 to your computer and use it in GitHub Desktop.
Save fj/2629588 to your computer and use it in GitHub Desktop.
Dynamic CSS in Rails 3.2

You operate a terrible clone of about.me called my.name. The entire purpose of the site is to show a user's name with some limited styling customization.

You allow users to configure a small number of parameters -- say, background color, font size, and some other properties -- and you store this information with each user.

When a request is made for my.name/123, you look up the user with id 123 and show that name in the middle of the page. The customized styling associated with each user is in the form of a CSS file that looks like styles.css?user_id=123. The CSS served is the same for any page; only the values for a few of the fields will be different for a given user_id.

You're using Rails 3.2 and Compass, so you want styles.css to actually be a dynamic file that generates an asset in the pipeline and which can be easily cached (so that you're not constantly recomputing styles.css?user_id=... for a given user).

What do you do?

@phillipkoebbe
Copy link

@fj Having external files to worry about is a pain. Been there, done that, don't want the book. In this case, however, since they are dynamically generated to begin with, I'd just make it part of the deployment process by creating a rake task and calling it from Capistrano.

I'm not sure how significant the special case is. You already have the special case of "Does this user have any customizations to consider?", so slight changes in the perspective of how you approach the special case might be negligible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment