New in Edge Rails: helper for creating form labels

In: Ruby on Rails|Web development

25 Sep 2007

Edge Rails now comes with a helper for creating form labels, so in your views, instead of doing this:

<% form_for(:user, @user, :url => user_path(@user)) do |f| -%>
<label for="user_login">Login</label>
<%= f.text_field :login %>

where the <label> is given a for attribute of user_login (since the convention for form input ids is {MODEL_NAME}_{ATTRIBUTE_NAME}), you can instead now do:

<%= label(:user, :login) %>

Quite a bit neater. Makes it easier to explain to beginners how they should use <label>s appropriately since the syntax is similar to the other form helpers. Now give your <label>s proper for attributes if you’re not doing so already.

This makes plugins like this form label helper plugin obsolete.

1 Response to New in Edge Rails: helper for creating form labels



September 25th, 2007 at 10pm

A little plug… I’ve been using web_sg_form_builder to get my forms markup nice and proper, and hadn’t looked back since.

Same markup (structure) for all forms, if there’s extra energy, expend it on the form CSS instead.