Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8641 (closed enhancement: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] label tag helpers for FormHelper and FormBuilder

Reported by: jcoglan Assigned to: core
Priority: normal Milestone: 1.x
Component: ActionPack Version: edge
Severity: normal Keywords: unverified
Cc: Zargony

Description

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 patch lets you do this by adding a label method to ActionView::Helpers::FormHelper and a to_label_tag method to ActionView::Helpers::InstanceTag. You can also use the FormBuilder style as shown above. Documentation and tests (included in the patch) explain what label does more fully.

Attachments

form_helper_labels.diff (5.0 kB) - added by jcoglan on 06/13/07 13:18:29.

Change History

06/13/07 10:55:00 changed by jcoglan

  • summary changed from label tag helpers for FormHelper and FormBuilder to [PATCH] label tag helpers for FormHelper and FormBuilder.

06/13/07 13:18:29 changed by jcoglan

  • attachment form_helper_labels.diff added.

07/30/07 17:24:23 changed by Zargony

  • cc set to Zargony.

I like the idea of having FormBuilder#label. That'd make label creation a lot easier in my case (where parts of my forms are nested with f.fields_for).

09/16/07 10:43:08 changed by jcoglan

  • keywords set to unverified.

09/16/07 21:27:26 changed by josh

Its causing some weird SQLite errors.

http://pastie.caboo.se/97764

What db where you using for testing.

09/17/07 00:04:55 changed by jcoglan

I usually run under MySQL but I installed sqlite 3.5.0 (on Windows) with the sqlite3-ruby gem for msin32, updated my Rails working copy and applied the patch. The tests in your pastie run fine for me. Anyone else having problems?

C:\webserver\Rails\rails\actionpack>ruby -Ilib:test "C:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb" "test/activerecord/active_record_store_test.rb" "test/activerecord/render_partial_with_record_identification_test.rb"
Attempting to load Active Record... success
Loaded suite C:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader
Started
...........................
Finished in 0.89 seconds.

27 tests, 45 assertions, 0 failures, 0 errors

09/20/07 23:32:27 changed by hvolkmer

+1

Tests work fine with sqlite3 3.4.2 on MacOSX

09/22/07 17:17:28 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [7541]) Added FormHelper#label (closes #8641) [jcoglan]