Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added FormHelper#radio_button to work with radio buttons like its alr…

…eady possible with check boxes [Michael Koziarski]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@151 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit c80f974fa3071104397d8dd5752ff60bca1881fe 1 parent e5a8d8e
David Heinemeier Hansson authored
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Added FormHelper#radio_button to work with radio buttons like its already possible with check boxes [Michael Koziarski]
  4
+
3 5
 * Added TemplateError#backtrace that makes it much easier to debug template errors from unit and functional tests
4 6
 
5 7
 * Added display of error messages with scaffolded form pages
23  actionpack/lib/action_view/helpers/form_helper.rb
@@ -99,6 +99,20 @@ def text_area(object, method, options = {})
99 99
       def check_box(object, method, options = {}, checked_value = "1", unchecked_value = "0")
100 100
         InstanceTag.new(object, method, self).to_check_box_tag(options, checked_value, unchecked_value)
101 101
       end
  102
+
  103
+      # Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
  104
+      # assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
  105
+      # radio button will be checked. Additional options on the input tag can be passed as a
  106
+      # hash with +options+. 
  107
+      # Example (call, result). Imagine that @post.category returns "rails":
  108
+      #   radio_button("post", "category", "rails")
  109
+      #   radio_button("post", "category", "java")
  110
+      #     <input type="radio" id="post_category" name="post[category] value="rails" checked="checked" />
  111
+      #     <input type="radio" id="post_category" name="post[category] value="java" />
  112
+      #     
  113
+      def radio_button(object, method, tag_value, options = {})
  114
+        InstanceTag.new(object, method, self).to_radio_button_tag(tag_value, options)
  115
+      end
102 116
     end
103 117
 
104 118
     class InstanceTag #:nodoc:
@@ -123,6 +137,15 @@ def to_input_field_tag(field_type, options = {})
123 137
         add_default_name_and_id(html_options)
124 138
         tag("input", html_options)
125 139
       end
  140
+
  141
+      def to_radio_button_tag(tag_value, options={})
  142
+        html_options = DEFAULT_FIELD_OPTIONS.merge(options)
  143
+        html_options.merge!({"checked"=>"checked"}) if value == tag_value
  144
+        html_options.merge!({"type"=>"radio", "value"=>tag_value.to_s})
  145
+                       
  146
+        add_default_name_and_id(html_options)
  147
+        tag("input", html_options)
  148
+      end
126 149
       
127 150
       def to_text_area_tag(options = {})
128 151
         options = DEFAULT_TEXT_AREA_OPTIONS.merge(options)
10  actionpack/test/template/form_helper_test.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require 'test/unit'
  2
+require 'erb'
2 3
 require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper'
3 4
 
4 5
 class FormHelperTest < Test::Unit::TestCase
@@ -70,6 +71,15 @@ def test_check_box
70 71
       check_box("post", "secret")
71 72
     )
72 73
   end
  74
+
  75
+  def test_radio_button
  76
+    assert_equal('<input checked="checked" id="post_title" name="post[title]" size="30" type="radio" value="Hello World" />',
  77
+      radio_button("post", "title", "Hello World") 
  78
+    )
  79
+    assert_equal('<input id="post_title" name="post[title]" size="30" type="radio" value="Goodbye World" />',
  80
+      radio_button("post", "title", "Goodbye World") 
  81
+    )
  82
+  end
73 83
   
74 84
   def test_text_area
75 85
     assert_equal(

0 notes on commit c80f974

Please sign in to comment.
Something went wrong with that request. Please try again.