Related Posts

Archive

Use the Name and Action of the Current Rails Controller for CSS Hooks

August 7th, 2008

I’ve started a few projects - and finished none of them - in Ruby on Rails 2.1. I got a little bit of experience in 1.2.x, enough to grow dependant on using scaffolding - though I have one major issue with the default scaffolding:

app/
views/
layouts/
wow.html.erb
that.html.erb
is.html.erb
a.html.erb
lot.html.erb
of.html.erb
layouts.html.erb

… when it could be

app/
views/
layouts/
application.html.erb

… without any other code modification; it results in a lot less markup to update if you have regular features.

But I want different CSS for different controllers, too. So I’ll add in some attributes to the body tag:

<body
id='<%= controller.class.controller_name %>_<%= controller.action_name %>'
class='<%= controller.class.controller_name %> <%= controller.action_name %>'>

If I was getting really specific, I’d add some prefixes; but this is enough for rules like:

#sidebar { background: blue; }
body.index #sidebar { background: red; }
body.customer #sidebar { background: green; }
body#customer_index #sidebar { display: none; }

Posted in Ruby |

Comments are closed.

Previous post: Use the PrototypeJS Browser Detect

Next Post: Firefox Addons (preferred set)