diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-14 13:48:27 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-14 13:48:27 +0000 |
commit | c80f974fa3071104397d8dd5752ff60bca1881fe (patch) | |
tree | 4d679c841f3ceddbeb15eb0610a1f2afec32d8c5 /actionpack | |
parent | e5a8d8ebbbb21dcd05d98d055dd0f75da972543e (diff) | |
download | rails-c80f974fa3071104397d8dd5752ff60bca1881fe.tar.gz rails-c80f974fa3071104397d8dd5752ff60bca1881fe.tar.bz2 rails-c80f974fa3071104397d8dd5752ff60bca1881fe.zip |
Added FormHelper#radio_button to work with radio buttons like its already possible with check boxes [Michael Koziarski]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@151 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/form_helper.rb | 23 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 10 |
3 files changed, 35 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 54d451ad71..7e81100979 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added FormHelper#radio_button to work with radio buttons like its already possible with check boxes [Michael Koziarski] + * Added TemplateError#backtrace that makes it much easier to debug template errors from unit and functional tests * Added display of error messages with scaffolded form pages diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index f64965bda1..0a406cfc47 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -99,6 +99,20 @@ module ActionView def check_box(object, method, options = {}, checked_value = "1", unchecked_value = "0") InstanceTag.new(object, method, self).to_check_box_tag(options, checked_value, unchecked_value) end + + # Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object + # assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the + # radio button will be checked. Additional options on the input tag can be passed as a + # hash with +options+. + # Example (call, result). Imagine that @post.category returns "rails": + # radio_button("post", "category", "rails") + # radio_button("post", "category", "java") + # <input type="radio" id="post_category" name="post[category] value="rails" checked="checked" /> + # <input type="radio" id="post_category" name="post[category] value="java" /> + # + def radio_button(object, method, tag_value, options = {}) + InstanceTag.new(object, method, self).to_radio_button_tag(tag_value, options) + end end class InstanceTag #:nodoc: @@ -123,6 +137,15 @@ module ActionView add_default_name_and_id(html_options) tag("input", html_options) end + + def to_radio_button_tag(tag_value, options={}) + html_options = DEFAULT_FIELD_OPTIONS.merge(options) + html_options.merge!({"checked"=>"checked"}) if value == tag_value + html_options.merge!({"type"=>"radio", "value"=>tag_value.to_s}) + + add_default_name_and_id(html_options) + tag("input", html_options) + end def to_text_area_tag(options = {}) options = DEFAULT_TEXT_AREA_OPTIONS.merge(options) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 8f3d5ebb94..d81e2ac270 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1,4 +1,5 @@ require 'test/unit' +require 'erb' require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' class FormHelperTest < Test::Unit::TestCase @@ -70,6 +71,15 @@ class FormHelperTest < Test::Unit::TestCase check_box("post", "secret") ) end + + def test_radio_button + assert_equal('<input checked="checked" id="post_title" name="post[title]" size="30" type="radio" value="Hello World" />', + radio_button("post", "title", "Hello World") + ) + assert_equal('<input id="post_title" name="post[title]" size="30" type="radio" value="Goodbye World" />', + radio_button("post", "title", "Goodbye World") + ) + end def test_text_area assert_equal( |