Way back when, I wrote about how to do Dirt Simple RCSS in Rails. Now that Rails 2.0 is upon us, it's time to get even simpler. With all the restful magic in Rails 2.0, you can get even simpler than dirt.

Let's assume you have a restful User resource. You've got your "map.resources" in routes.rb, a typical User model, and a Users controller with standard views like index and show. Now you want to generate user-specific css based on state in the user model.

1) Create a view template called "show.css.erb". For example:

p { color: <%= @user.color %>; }

2) Add a css format option to the respond_to block in the show action of the UsersController. You only need the default behavior for the css format, since all you need to do is render the view.

def show @user = User.find(params[:id]) respond_to do |format| format.html format.css end end

You can see the dynamically generated css at a url like /users/1.css in your browser.

Here's how to use the stylesheet in a view, assuming you've set up a @user object:

<%= stylesheet_link_tag formatted_user_path(@user, "css") %>

Told you it was simple.

Note: Geoffrey "topfunky" Grosenbach also has a good article on dynamic CSS, which includes a nice screencast as well.