September 29, 2007

Posted by John

Older: I Can Has Command Line?

Newer: Easy Edge Rails Deployment

Label Helper Makes It Into Edge Rails

Changeset 7541 just made me a happy man. The first thing that I do with every rails I app I work on is install Rick Olson’s label helper plugin. With the aforementioned changeset though, a helper for making form labels has been added to rails core. This will save me time on each rails app which means time to market is now around 4 minutes. :)

A Few Examples

label(:post, :title) 
#=> <label for="post_title">Title</label> 

label(:post, :title, "A short title") 
#=> <label for="post_title">A short title</label> 

label(:post, :title, "A short title", :class => "title_label") 
#=> <label for="post_title" class="title_label">A short title</label>

I do find it odd that they didn’t add label’s partner, label_tag. All the other form helpers (text_field => text_field_tag, select => select_tag, etc.) have an _tag companion method. Maybe I missed it somewhere.

For those that don’t use labels, you should. They are good for accessibility and also when clicked, highlight the associated field element, if you code them correctly.

4 Comments

  1. It doesn’t look like this initial version of the helper takes a block parameter: the alternate use of

  2. Good point. Hadn’t thought of using a block for that.

  3. Could expand on this comment for me: “They are good for accessibility and also when clicked, highlight the associated field element, if you code them correctly.” I have used labels and know they are a good idea but never really knew how to make them more useful.

    Thanks

  4. @Brian Howard – Accessbility: Screen readers can tell which form element the label is related to if the label has a for attribute and the element has an id that is in the for attribute. For example…

    &lt;label for="user_name"&gt;Name:&lt;/label&gt;
    &lt;input type="text" id="user_name" name="user[name]" /&gt;

    If the for attribute lines up with the form element id then, in most browsers, you can click the label text and it will put the cursor in the form element or make it active if it is a select, option or check box.

    The other way of using labels is just to wrap them around the form control like so…

    &lt;label&gt;Name: &lt;input type="text" id="user_name" name="user[name]" /&gt;&lt;/label&gt;

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.