Label me, please!
Posted by Eric Stewart Fri, 13 Jan 2006 23:21:00 GMT
Imagine you are a good web developer. You like following standards, paying attention to usability, and thinking about accessibility while implementing the latest, greatest whiz-bang next-generation web wunder-site. Yes, I’m talking to you!
You have forms somewhere on your site, right? There has to be one somewhere. And you have labels on all of your fields too, right? Of course you do!
Well, label helpers are one of the things left out of Rails up to and including 1.0. It’s easy to imagine why. A label is pretty trivial to write, and if you can’t figure out the necessary for attribute, then all hope is lost. However you’d love to not have to worry about the for wouldn’t you?
Still, there really needed to be some other reason to go to all the trouble to write a form helper. Well, I can think of one. And so I did.
label_helpers plugin for Rails
This plugin adds the helpers label and label_tag. Of course, label_tag is quite trivial, so it’s label that you’ll see the most benefit from.
I happen to work on web sites often where accessibility is of high priority, and so anything I can to do further that effort is a plus. I happened to like the technique described by Derek Featherstone for form error messages.
In a nutshell, this technique and the new helper allow including error messages for validation problems within the label for an inputs. This allows the error message to be grouped with the field title, aiding screen readers as well as visual users. And CSS can be used to position the error portion of the label appropriately.
Of course you can turn the errors off if need be.
It’s still rough around the edges, but working for me so far. Comments are appreciated!
See the README for usage information.
Installing
Check out the source from Subversion, or preferable use Rails own script/plugin script to help install it. All you have to do (from the root of your rails project) is:
script/plugin source http://www.eric-stewart.com/svn/rails/plugins/
script/plugin install label_helpers
Or, for those that want to do a straight subversion checkout:
svn co http://www.eric-stewart.com/svn/rails/plugins/label_helpers/
<!- technorati tags start ->
Technorati Tags: accessibility, rails, ruby
<!





Works great, except that I had to comment out lines 49-53 (the error message lines).
If I leave them in then I receive NoMethod errors on line 52 for error_message.
HTH