Block label helper

3 views
Skip to first unread message

Stephen Celis

unread,
Jan 4, 2010, 9:34:17 AM1/4/10
to Ruby on Rails: Core
I've created a patch that lets the "label_tag" and "label" helpers
accept blocks. I've found that it's quite common for labels to include
markup beyond text, and things get messy quickly. Consider

<%= f.label :terms, "<span>Accept #{link_to 'Terms', terms_path}</
span>" %>

versus

<% f.label :terms do %>
<span>Accept <%= link_to "Terms", terms_path %></span>
<% end %>

Ticket with patch/tests:

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3645-let-label-helpers-accept-blocks

Stephen

Chris Cruft

unread,
Jan 5, 2010, 8:36:19 AM1/5/10
to Ruby on Rails: Core
Double bonus for this enhancement -thanks Stephen. I see that you
have also made the "name" attribute optional -fantastic. Now one can
implicitly apply a label to the enclosed input without having to worry
about id matching with the 'for' attribute:

label_tag nil, "Your Name" do
text_field_tag 'name'
end

IMO, the signature of #label_tag (and #label) should evolve to better
support this practice as it reduces the need to keep DOM ids in sync
between the input and the label. Perhaps by counting args to
label_tag and assuming a single string argument is a contextual label.

> https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3...
>
> Stephen

Stephen Celis

unread,
Jan 19, 2010, 9:07:00 AM1/19/10
to Ruby on Rails: Core
Thanks for taking a look, Chris. Could others offer their opinions?

Stephen

> >https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3645-let-label-helpers-accept-blocks
>
> > Stephen

Reply all
Reply to author
Forward
0 new messages