Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Let label helpers accept blocks

#3645

Label helpers should process blocks in addition to their defaults because it's common for their content to have more complex markup than text.

This, for example,

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

Is much easier to read in a block

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

An additional perk: in our editors, the HTML markup won't be lost in Ruby string syntax highlighting.

Patch with tests in comments.

Reported by Stephen Celis · January 3rd, 2010 @ 05:25 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Stephen Celis
    Stephen Celis
    • Tag set to action_view, block, form_helper, form_tag_helper, label, label_tag, to_label_tag

    January 3rd, 2010 @ 05:26 PM

  2. Stephen Celis
    Stephen Celis
    • Tag changed from action_view, block, form_helper, form_tag_helper, label, label_tag, to_label_tag to action_view, block, form_helper, form_tag_helper, label, label_tag, patch, to_label_tag

    January 3rd, 2010 @ 05:27 PM

  3. Stephen Celis
  4. Chris Hapgood
    Chris Hapgood

    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.

    January 5th, 2010 @ 01:36 PM

  5. Stefano Diem
    Stefano Diem

    Pretty cool!

    I use a lot of labels with images inside, especially on radiobuttons and checkboxes to be able to check the corresponding field without having to resort to javascript, and things usually get very messy having to add them inside the label and label_tag methods. So this patch is something i'm realy looking for! Do you need help with anything?

    February 5th, 2010 @ 12:49 PM

  6. Stephen Celis
    Stephen Celis

    Biggest help would be to test the patch out and get others to do so.

    Once a few people approve, we can mark this "confirmed."

    February 8th, 2010 @ 02:55 PM

  7. Stephen Celis
    Stephen Celis

    Updated patch rebased against master.

    April 21st, 2010 @ 05:19 AM

  8. José Valim
    José Valim
    • Milestone cleared.
    • Assigned user set to José Valim

    May 5th, 2010 @ 07:48 PM

  9. Adrian Sanchez
    Adrian Sanchez

    It's a good feature,

    I try, run the test and work fine.

    Thank you Stephen

    May 5th, 2010 @ 09:34 PM

  10. Dan Pickett
    Dan Pickett
    • Tag changed from action_view, block, form_helper, form_tag_helper, label, label_tag, patch, to_label_tag to action_view, block, bugmash, form_helper, form_tag_helper, label, label_tag, patch, to_label_tag

    May 9th, 2010 @ 06:52 PM

  11. Jeff Kreeftmeijer
    Jeff Kreeftmeijer

    +1

    Applies cleanly to master, great patch!

    May 14th, 2010 @ 01:02 PM

  12. Lake
    Lake

    +1

    Applies cleanly to master and tests pass. Nice work.

    May 14th, 2010 @ 08:19 PM

  13. Stephen Celis
    Stephen Celis
    • Tag changed from action_view, block, bugmash, form_helper, form_tag_helper, label, label_tag, patch, to_label_tag to action_view, block, bugmash, form_helper, form_tag_helper, label, label_tag, patch, to_label_tag, verified

    May 14th, 2010 @ 08:29 PM

  14. Repository
  15. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM