LabelHelper

Note: This plugin’s features are now in Rails core and this plugin will no longer be necessary when Rails 2.0 comes out.

Installation

script/plugin install
    http://svn.jcoglan.com/labelhelper/trunk/label_helper

Rails has no helper for label tags, which are important accessibility/usability features of forms. You shouldn‘t have to repeat yourself when writing labels for things: currently you need to do:

  <% form_for(:post, ...) do |f| %>
    <label for="post_title">Title</label>
    <%= f.text_field(:title) %>
    <label for="post_body">Body</label>
    <%= f.text_area(:body) %>
  <% end %>

Whereas I‘d rather not repeat myself over and over…

  <% form_for(:post, ...) do |f| %>
    <%= f.label(:title) %>
    <%= f.text_field(:title) %>
    <%= f.label(:body) %>
    <%= f.text_area(:body) %>
  <% end %>

This plugin lets you do this by adding a label method to ActionView::Helpers::FormHelper. You can also use the FormBuilder style as shown above.

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>

License

Copyright (c) 2007 James Coglan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.