From 34e153e03f6f797ca34e9e8ab12254b9e4e40293 Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Sat, 12 Feb 2011 12:07:47 +0000 Subject: [PATCH 2/2] Add block support to button_tag helper As per the HTML 4.01 spec: Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to "image", but the BUTTON element type allows content. Since rich content is the main purpose of the # - # button_tag "Ask me!", :type => 'button' + # button_tag(:type => 'button') do + # content_tag(:strong, 'Ask me!') + # end # # => @@ -442,7 +445,9 @@ module ActionView # # => # - def button_tag(label = "Button", options = {}) + def button_tag(content_or_options = nil, options = nil, &block) + options = content_or_options if block_given? && content_or_options.is_a?(Hash) + options ||= {} options.stringify_keys! if disable_with = options.delete("disable_with") @@ -455,7 +460,7 @@ module ActionView options.reverse_merge! 'name' => 'button', 'type' => 'submit' - content_tag :button, label, { "type" => options.delete("type") }.update(options) + content_tag :button, content_or_options || 'Button', options, &block end # Displays an image which when clicked will submit the form. diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index c22af25..f8671f2 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -427,6 +427,15 @@ class FormTagHelperTest < ActionView::TestCase ) end + def test_button_tag_with_block + assert_dom_equal('', button_tag { 'Content' }) + end + + def test_button_tag_with_block_and_options + output = button_tag(:name => 'temptation', :type => 'button') { content_tag(:strong, 'Do not press me') } + assert_dom_equal('', output) + end + def test_image_submit_tag_with_confirmation assert_dom_equal( %(), -- 1.7.2