Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Expanded :method option in FormHelper#form_tag to allow for verbs oth…

…er than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4371 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 062845b4da06f7025d6190899cde25deb8eaac42 1 parent 0b1d6fd
@dhh dhh authored
View
2  actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Expanded :method option in FormHelper#form_tag to allow for verbs other than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post [DHH]
+
* Added :method option to UrlHelper#link_to, which allows for using other verbs than GET for the link. This replaces the :post option, which is now deprecated. Example: link_to "Destroy", person_url(:id => person), :method => :delete [DHH]
* follow_redirect doesn't complain about being redirected to the same controller. #5153 [dymo@mk.ukrtelecom.ua]
View
21 actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -14,12 +14,27 @@ module FormTagHelper
#
# Options:
# * <tt>:multipart</tt> - If set to true, the enctype is set to "multipart/form-data".
- # * <tt>:method</tt> - The method to use when submitting the form, usually either "get" or "post".
+ # * <tt>:method</tt> - The method to use when submitting the form, usually either "get" or "post".
+ # If "put", "delete", or another verb is used, a hidden input with name _method
+ # is added to simulate the verb over post.
def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc)
- html_options = { "method" => "post" }.merge(options.stringify_keys)
+ html_options = options.stringify_keys
html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart")
html_options["action"] = url_for(url_for_options, *parameters_for_url)
- tag :form, html_options, true
+
+ method_tag = ""
+
+ case method = html_options.delete("method")
+ when /^get$/i # must be case-insentive, but can't use downcase as might be nil
+ html_options["method"] = "get"
+ when /^post$/i, nil
+ html_options["method"] = "post"
+ else
+ html_options["method"] = "post"
+ method_tag = tag(:input, :type => "hidden", :name => "_method", :value => method)
+ end
+
+ tag(:form, html_options, true) + method_tag
end
alias_method :start_form_tag, :form_tag
View
21 actionpack/test/template/form_helper_test.rb
@@ -239,6 +239,27 @@ def test_form_for
assert_dom_equal expected, _erbout
end
+ def test_form_for_with_method
+ _erbout = ''
+
+ form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
+ _erbout.concat f.text_field(:title)
+ _erbout.concat f.text_area(:body)
+ _erbout.concat f.check_box(:secret)
+ end
+
+ expected =
+ "<form action='http://www.example.com' id='create-post' method='post'>" +
+ "<input name='_method' type='hidden' value='put' />" +
+ "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
+ "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
+ "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
+ "<input name='post[secret]' type='hidden' value='0' />" +
+ "</form>"
+
+ assert_dom_equal expected, _erbout
+ end
+
def test_form_for_without_object
_erbout = ''
View
6 actionpack/test/template/form_tag_helper_test.rb
@@ -33,6 +33,12 @@ def test_form_tag_multipart
assert_dom_equal expected, actual
end
+ def test_form_tag_with_method
+ actual = form_tag({}, { :method => :put })
+ expected = %(<form action="http://www.example.com" method="post"><input type="hidden" name="_method" value="put" />)
+ assert_dom_equal expected, actual
+ end
+
def test_hidden_field_tag
actual = hidden_field_tag "id", 3
expected = %(<input id="id" name="id" type="hidden" value="3" />)
Please sign in to comment.
Something went wrong with that request. Please try again.