Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7413 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 0e6c8e5f6c168b9d376122f730c604d3758f4b04 1 parent 8a9f43e
@NZKoz NZKoz authored
View
2  actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]
+
* Allow additional parameters to be passed to named route helpers when using positional arguments. Closes #8930 [ian.w.white@gmail.com]
* Make render :partial work with a :collection of Hashes, previously this wasn't possible due to backwards compatibility restrictions. [lifofifo]
View
22 actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -363,6 +363,28 @@ def submit_tag(value = "Save changes", options = {})
def image_submit_tag(source, options = {})
tag :input, { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys)
end
+
+ # Creates a field set for grouping HTML form elements.
+ #
+ # <tt>legend</tt> will become the fieldset's title (optional as per W3C).
+ #
+ # === Examples
+ # <% fieldset_tag do %>
+ # <p><%= text_field_tag 'name' %></p>
+ # <% end %>
+ # # => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>
+ #
+ # <% fieldset_tag 'Your details' do %>
+ # <p><%= text_field_tag 'name' %></p>
+ # <% end %>
+ # # => <fieldset><legend>Your details</legend><p><input id="name" name="name" type="text" /></p></fieldset>
+ def fieldset_tag(legend = nil, &block)
+ content = capture(&block)
+ concat(tag(:fieldset, {}, true), block.binding)
+ concat(content_tag(:legend, legend), block.binding) unless legend.blank?
+ concat(content, block.binding)
+ concat("</fieldset>", block.binding)
+ end
private
def html_options_for_form(url_for_options, options, *parameters_for_url)
View
20 actionpack/test/template/form_tag_helper_test.rb
@@ -157,4 +157,24 @@ def test_submit_tag
def test_pass
assert_equal 1, 1
end
+
+ def test_fieldset_tag
+ _erbout = ''
+ fieldset_tag("Your details") { _erbout.concat "Hello world!" }
+
+ expected = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
+ assert_dom_equal expected, _erbout
+
+ _erbout = ''
+ fieldset_tag { _erbout.concat "Hello world!" }
+
+ expected = %(<fieldset>Hello world!</fieldset>)
+ assert_dom_equal expected, _erbout
+
+ _erbout = ''
+ fieldset_tag('') { _erbout.concat "Hello world!" }
+
+ expected = %(<fieldset>Hello world!</fieldset>)
+ assert_dom_equal expected, _erbout
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.