public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
FormHelper#label_tag accepts :for option [encoded] [#38 state:resolved]
josh (author)
Wed Apr 30 15:14:28 -0700 2008
commit  96d9691e71319f4c166315a36b96c2c3c54ed493
tree    43bf8e70dc9cce11531a7ce9e017b7c11a1de95d
parent  b4c33711c52dc78e6ff63469a5caa89f9a67c61a
...
1
2
3
 
4
5
6
...
51
52
53
54
 
55
56
57
...
91
92
93
94
 
95
96
97
...
149
150
151
152
 
153
154
155
...
337
338
339
340
 
341
342
343
...
404
405
406
407
 
408
409
410
...
445
446
447
448
 
449
450
451
...
467
468
469
 
470
471
472
 
473
474
 
475
476
477
...
1
2
3
4
5
6
7
...
52
53
54
 
55
56
57
58
...
92
93
94
 
95
96
97
98
...
150
151
152
 
153
154
155
156
...
338
339
340
 
341
342
343
344
...
405
406
407
 
408
409
410
411
...
446
447
448
 
449
450
451
452
...
468
469
470
471
472
473
 
474
475
 
476
477
478
479
0
@@ -1,6 +1,7 @@
0
 require 'cgi'
0
 require 'action_view/helpers/date_helper'
0
 require 'action_view/helpers/tag_helper'
0
+require 'action_view/helpers/form_tag_helper'
0
 
0
 module ActionView
0
   module Helpers
0
@@ -51,7 +52,7 @@ module ActionView
0
     #
0
     # If the object name contains square brackets the id for the object will be inserted. For example:
0
     #
0
- # <%= text_field "person[]", "name" %>
0
+ # <%= text_field "person[]", "name" %>
0
     #
0
     # ...will generate the following ERb.
0
     #
0
@@ -91,7 +92,7 @@ module ActionView
0
       #
0
       # Even further, the form_for method allows you to more easily escape the instance variable convention. So while the stand-alone
0
       # approach would require <tt>text_field :person, :name, :object => person</tt>
0
- # to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with
0
+ # to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with
0
       # <tt>:person, person</tt> and all subsequent field calls save <tt>:person</tt> and <tt>:object => person</tt>.
0
       #
0
       # Also note that form_for doesn't create an exclusive scope. It's still possible to use both the stand-alone FormHelper methods
0
@@ -149,7 +150,7 @@ module ActionView
0
       # ...
0
       # <% end %>
0
       #
0
- # And for namespaced routes, like admin_post_url:
0
+ # And for namespaced routes, like admin_post_url:
0
       #
0
       # <% form_for([:admin, @post]) do |f| %>
0
       # ...
0
@@ -337,7 +338,7 @@ module ActionView
0
       # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
0
       # shown.
0
       #
0
- # ==== Examples
0
+ # ==== Examples
0
       # hidden_field(:signup, :pass_confirm)
0
       # # => <input type="hidden" id="signup_pass_confirm" name="signup[pass_confirm]" value="#{@signup.pass_confirm}" />
0
       #
0
@@ -404,7 +405,7 @@ module ActionView
0
       # is set to 0 which is convenient for boolean values. Since HTTP standards say that unchecked checkboxes don't post anything,
0
       # we add a hidden value with the same name as the checkbox as a work around.
0
       #
0
- # ==== Examples
0
+ # ==== Examples
0
       # # Let's say that @post.validated? is 1:
0
       # check_box("post", "validated")
0
       # # => <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
0
@@ -445,7 +446,7 @@ module ActionView
0
     end
0
 
0
     class InstanceTag #:nodoc:
0
- include Helpers::TagHelper
0
+ include Helpers::TagHelper, Helpers::FormTagHelper
0
 
0
       attr_reader :method_name, :object_name
0
 
0
@@ -467,11 +468,12 @@ module ActionView
0
       end
0
 
0
       def to_label_tag(text = nil, options = {})
0
+ options = options.stringify_keys
0
         name_and_id = options.dup
0
         add_default_name_and_id(name_and_id)
0
- options["for"] = name_and_id["id"]
0
+ options["for"] ||= name_and_id["id"]
0
         content = (text.blank? ? nil : text.to_s) || method_name.humanize
0
- content_tag("label", content, options)
0
+ label_tag(name_and_id["id"], content, options)
0
       end
0
 
0
       def to_input_field_tag(field_type, options = {})
...
6
7
8
9
 
10
11
12
13
 
14
15
16
...
36
37
38
39
40
41
42
 
 
 
 
43
44
45
 
 
46
47
48
...
72
73
74
75
 
76
77
78
79
 
 
 
 
 
 
 
 
80
81
82
...
303
304
305
306
 
307
308
309
...
325
326
327
328
 
329
330
331
...
346
347
348
349
 
350
351
352
...
367
368
369
370
 
371
372
373
...
423
424
425
426
 
427
428
429
...
494
495
496
497
 
498
499
500
...
511
512
513
514
 
515
516
517
...
548
549
550
551
 
552
553
554
...
571
572
573
574
 
575
576
577
...
601
602
603
604
 
605
606
607
...
623
624
625
626
 
627
628
629
...
637
638
639
640
641
 
 
642
643
644
645
646
647
648
 
 
 
 
649
650
651
 
652
653
654
655
 
656
657
658
659
660
 
 
661
662
663
664
665
666
667
 
 
 
 
668
669
670
 
671
672
 
673
674
675
...
683
684
685
686
 
687
688
689
...
693
694
695
696
 
697
698
699
 
700
701
702
703
704
705
706
 
 
707
708
709
710
711
 
712
713
714
715
716
717
 
718
719
720
 
721
722
723
...
793
794
795
796
 
797
798
799
800
 
801
802
803
804
805
 
806
807
808
...
819
820
821
822
 
823
824
825
 
826
827
828
...
837
838
839
840
 
841
842
843
844
845
 
846
847
848
849
850
 
851
852
853
854
 
 
855
856
857
...
6
7
8
 
9
10
11
12
 
13
14
15
16
...
36
37
38
 
 
 
 
39
40
41
42
43
 
 
44
45
46
47
48
...
72
73
74
 
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
...
311
312
313
 
314
315
316
317
...
333
334
335
 
336
337
338
339
...
354
355
356
 
357
358
359
360
...
375
376
377
 
378
379
380
381
...
431
432
433
 
434
435
436
437
...
502
503
504
 
505
506
507
508
...
519
520
521
 
522
523
524
525
...
556
557
558
 
559
560
561
562
...
579
580
581
 
582
583
584
585
...
609
610
611
 
612
613
614
615
...
631
632
633
 
634
635
636
637
...
645
646
647
 
 
648
649
650
651
652
 
 
 
 
653
654
655
656
657
658
 
659
660
661
662
 
663
664
665
666
 
 
667
668
669
670
671
 
 
 
 
672
673
674
675
676
677
 
678
679
 
680
681
682
683
...
691
692
693
 
694
695
696
697
...
701
702
703
 
704
705
706
 
707
708
709
710
711
712
 
 
713
714
715
716
717
718
 
719
720
721
722
723
724
 
725
726
727
 
728
729
730
731
...
801
802
803
 
804
805
806
807
 
808
809
810
811
812
 
813
814
815
816
...
827
828
829
 
830
831
832
 
833
834
835
836
...
845
846
847
 
848
849
850
851
852
 
853
854
855
856
857
 
858
859
860
 
 
861
862
863
864
865
0
@@ -6,11 +6,11 @@ silence_warnings do
0
     alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
0
     alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
0
     alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
0
-
0
+
0
     def new_record=(boolean)
0
       @new_record = boolean
0
     end
0
-
0
+
0
     def new_record?
0
       @new_record
0
     end
0
@@ -36,13 +36,13 @@ class FormHelperTest < ActionView::TestCase
0
   def setup
0
     @post = Post.new
0
     @comment = Comment.new
0
- def @post.errors()
0
- Class.new{
0
- def on(field); "can't be empty" if field == "author_name"; end
0
- def empty?() false end
0
+ def @post.errors()
0
+ Class.new{
0
+ def on(field); "can't be empty" if field == "author_name"; end
0
+ def empty?() false end
0
         def count() 1 end
0
- def full_messages() [ "Author name can't be empty" ] end
0
- }.new
0
+ def full_messages() [ "Author name can't be empty" ] end
0
+ }.new
0
     end
0
     def @post.id; 123; end
0
     def @post.id_before_type_cast; 123; end
0
@@ -72,11 +72,19 @@ class FormHelperTest < ActionView::TestCase
0
       label("post", "title", nil, :class => 'title_label')
0
     )
0
   end
0
-
0
+
0
   def test_label_with_symbols
0
     assert_dom_equal('<label for="post_title">Title</label>', label(:post, :title))
0
   end
0
 
0
+ def test_label_with_for_attribute_as_symbol
0
+ assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, :for => "my_for"))
0
+ end
0
+
0
+ def test_label_with_for_attribute_as_string
0
+ assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, "for" => "my_for"))
0
+ end
0
+
0
   def test_text_field
0
     assert_dom_equal(
0
       '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
0
@@ -303,7 +311,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.submit('Create post')
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' id='create-post' method='post'>" +
0
       "<label for='post_title'>Title</label>" +
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
@@ -325,7 +333,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' id='create-post' method='post'>" +
0
       "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
@@ -346,7 +354,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' id='create-post' method='post'>" +
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
0
@@ -367,7 +375,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' method='post'>" +
0
       "<label for=\"post_123_title\">Title</label>" +
0
       "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
0
@@ -423,7 +431,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
0
       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
0
@@ -494,7 +502,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
0
       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
0
@@ -511,7 +519,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
0
       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
0
@@ -548,7 +556,7 @@ class FormHelperTest < ActionView::TestCase
0
       end
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' id='create-post' method='post'>" +
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
0
@@ -571,7 +579,7 @@ class FormHelperTest < ActionView::TestCase
0
       end
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' id='create-post' method='post'>" +
0
       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
0
       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
0
@@ -601,7 +609,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' method='post'>" +
0
       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
0
       "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
0
@@ -623,7 +631,7 @@ class FormHelperTest < ActionView::TestCase
0
       _erbout.concat f.check_box(:secret)
0
     end
0
 
0
- expected =
0
+ expected =
0
       "<form action='http://www.example.com' method='post'>" +
0
       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
0
       "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
0
@@ -637,39 +645,39 @@ class FormHelperTest < ActionView::TestCase
0
   end
0
 
0
   def test_default_form_builder_with_active_record_helpers
0
-
0
- _erbout = ''
0
+
0
+ _erbout = ''
0
     form_for(:post, @post) do |f|
0
        _erbout.concat f.error_message_on('author_name')
0
        _erbout.concat f.error_messages
0
- end
0
-
0
- expected = %(<form action='http://www.example.com' method='post'>) +
0
- %(<div class='formError'>can't be empty</div>) +
0
+ end
0
+
0
+ expected = %(<form action='http://www.example.com' method='post'>) +
0
+ %(<div class='formError'>can't be empty</div>) +
0
                %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
0
                %(</form>)
0
-
0
+
0
     assert_dom_equal expected, _erbout
0
 
0
   end
0
-
0
+
0
   def test_default_form_builder_no_instance_variable
0
     post = @post
0
     @post = nil
0
-
0
- _erbout = ''
0
+
0
+ _erbout = ''
0
     form_for(:post, post) do |f|
0
        _erbout.concat f.error_message_on('author_name')
0
        _erbout.concat f.error_messages
0
- end
0
-
0
- expected = %(<form action='http://www.example.com' method='post'>) +
0
- %(<div class='formError'>can't be empty</div>) +
0
+ end
0
+
0
+ expected = %(<form action='http://www.example.com' method='post'>) +
0
+ %(<div class='formError'>can't be empty</div>) +
0
                %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
0
                %(</form>)
0
-
0
+
0
     assert_dom_equal expected, _erbout
0
-
0
+
0
   end
0
 
0
   # Perhaps this test should be moved to prototype helper tests.
0
@@ -683,7 +691,7 @@ class FormHelperTest < ActionView::TestCase
0
        _erbout.concat f.check_box(:secret)
0
      end
0
 
0
- expected =
0
+ expected =
0
        %(<form action="http://www.example.com" onsubmit="new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post">) +
0
        "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
0
        "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
0
@@ -693,31 +701,31 @@ class FormHelperTest < ActionView::TestCase
0
 
0
      assert_dom_equal expected, _erbout
0
   end
0
-
0
+
0
   def test_fields_for_with_labelled_builder
0
     _erbout = ''
0
-
0
+
0
     fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
0
       _erbout.concat f.text_field(:title)
0
       _erbout.concat f.text_area(:body)
0
       _erbout.concat f.check_box(:secret)
0
     end
0
-
0
- expected =
0
+
0
+ expected =
0
       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
0
       "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
0
       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
0
       "<input name='post[secret]' type='hidden' value='0' /><br/>"
0
-
0
+
0
     assert_dom_equal expected, _erbout
0
   end
0
 
0
   def test_form_for_with_html_options_adds_options_to_form_tag
0
     _erbout = ''
0
-
0
+
0
     form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
0
     expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
0
-
0
+
0
     assert_dom_equal expected, _erbout
0
   end
0
 
0
@@ -793,16 +801,16 @@ class FormHelperTest < ActionView::TestCase
0
     @comment.save
0
     _erbout = ''
0
     form_for([:admin, @post, @comment]) {}
0
-
0
+
0
     expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
0
     assert_dom_equal expected, _erbout
0
   end
0
-
0
+
0
   def test_form_for_with_new_object_and_namespace_in_list
0
     @post.new_record = false
0
     _erbout = ''
0
     form_for([:admin, @post, @comment]) {}
0
-
0
+
0
     expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
0
     assert_dom_equal expected, _erbout
0
   end
0
@@ -819,10 +827,10 @@ class FormHelperTest < ActionView::TestCase
0
   def test_remote_form_for_with_html_options_adds_options_to_form_tag
0
     self.extend ActionView::Helpers::PrototypeHelper
0
     _erbout = ''
0
-
0
+
0
     remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
0
     expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\" onsubmit=\"new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"></form>"
0
-
0
+
0
     assert_dom_equal expected, _erbout
0
   end
0
 
0
@@ -837,21 +845,21 @@ class FormHelperTest < ActionView::TestCase
0
       "/posts/#{post.id}/comments/#{comment.id}"
0
     end
0
     alias_method :post_comment_path, :comment_path
0
-
0
+
0
     def admin_comments_path(post)
0
       "/admin/posts/#{post.id}/comments"
0
     end
0
     alias_method :admin_post_comments_path, :admin_comments_path
0
-
0
+
0
     def admin_comment_path(post, comment)
0
       "/admin/posts/#{post.id}/comments/#{comment.id}"
0
     end
0
     alias_method :admin_post_comment_path, :admin_comment_path
0
-
0
+
0
     def posts_path
0
       "/posts"
0
- end
0
-
0
+ end
0
+
0
     def post_path(post)
0
       "/posts/#{post.id}"
0
     end

Comments

    No one has commented yet.