Wednesday, May 21st, 2008

classy_inputs: Rails plugin to add autoclass names

Category: CSS, Ruby

We all want to use input[type=text] but browser support doesn’t seem to quite be there (IE 6?).

This lead James Coglan down the path of creating a teeny Rails plugin, classy_inputs:

Good lord do I ever hate input tags. (YUI hates them too, but I’ll leave that story for another time). All the different types should really have been different tag names, and they are a total pain to use with CSS. As such, I used to end up doing tedious stuff like adding a :class option to every form element when writing Rails templates. A while back, I tried to patch Rails to get it to do this automatically, but its test suite totally baffled me at the time by applying the classes in some places but not others.

So, a quick plugin to stave off RSI:

script/plugin install

With that installed, any input tag created using a Rails view method will gain a class name matching its type attribute. Maybe I’ll have another stab at patching Rails, but I’m not promising anything.

Posted by Dion Almaer at 9:50 am

2.8 rating from 11 votes


Comments feed TrackBack URI

A nice idea … I usually just add classnames to the enclosing fieldsets … and someday, I’ll take a stab at patching Rails scaffold code to use fieldsets instead of p tags … at least they’re using labels and not spans.

Comment by MorganRoderick — May 21, 2008

Why would you use a fieldset for a single field and its label?

Fieldsets are for field sets.

Comment by ceejayoz — May 21, 2008

With IE6 losing it’s share now, I’ve started to finally adopt CSS selectors to style inputs. On IE6 and worse, you simply do not see them, while on better browsers you get the full eyecandy.

Comment by Gordon — May 21, 2008

I’m with James on this. I’ve actually been pondering writing a plugin that does the exact same thing as this.

Also I’ve kind of been doing what Gordon has been doing by using jquery to add the class name but that means you have to wait for the dom to load until you can apply the class styles.

Comment by JimNeath — May 22, 2008

If you check the various Rails plugin archives you will find there are at least three or four plugins that do this same thing already. Been using them for quite a while.

Comment by eric2 — May 22, 2008

eric2: how about posting a link or three?

i’ve also been thinking about doing the same for CakePHP

Comment by naterkane — May 22, 2008

Leave a comment

You must be logged in to post a comment.