Browse files

button_to accepts :method so you can PUT and DELETE with it. Closes #…

…6005.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4914 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f6339eb commit a769b88819ac1ad1d89a1f9138bf1546091d3744 @jeremy jeremy committed Sep 3, 2006
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* button_to accepts :method so you can PUT and DELETE with it. #6005 [Dan Webb]
+
* Update sanitize text helper to strip plaintext tags, and <img src="javascript:bang">. [Rick Olson]
* Update routing documentation. Closes #6017 [Nathan Witmer]
View
23 actionpack/lib/action_view/helpers/url_helper.rb
@@ -93,12 +93,14 @@ def link_to(name, options = {}, html_options = nil, *parameters_for_method_refer
# Example 2:
#
# button_to "Destroy", { :action => 'destroy', :id => 3 },
- # :confirm => "Are you sure?"
+ # :confirm => "Are you sure?", :method => :delete
#
# Generates the following HTML (sans formatting):
#
# <form method="post" action="/feeds/destroy/3" class="button-to">
- # <div><input onclick="return confirm('Are you sure?');"
+ # <div>
+ # <input type="hidden" name="_method" value="delete" />
+ # <input onclick="return confirm('Are you sure?');"
# value="Destroy" type="submit" />
# </div>
# </form>
@@ -113,18 +115,25 @@ def link_to(name, options = {}, html_options = nil, *parameters_for_method_refer
def button_to(name, options = {}, html_options = nil)
html_options = (html_options || {}).stringify_keys
convert_boolean_attributes!(html_options, %w( disabled ))
-
+
+ method_tag = ''
+ if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
+ method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
+ end
+
+ form_method = method.to_s == 'get' ? 'get' : 'post'
+
if confirm = html_options.delete("confirm")
html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
end
-
+
url = options.is_a?(String) ? options : url_for(options)
name ||= url
-
+
html_options.merge!("type" => "submit", "value" => name)
- "<form method=\"post\" action=\"#{h url}\" class=\"button-to\"><div>" +
- tag("input", html_options) + "</div></form>"
+ "<form method=\"#{form_method}\" action=\"#{h url}\" class=\"button-to\"><div>" +
+ method_tag + tag("input", html_options) + "</div></form>"
end
View
14 actionpack/test/template/url_helper_test.rb
@@ -59,6 +59,20 @@ def test_button_to_enabled_disabled
button_to("Hello", "http://www.example.com", :disabled => true)
)
end
+
+ def test_button_to_with_method_delete
+ assert_dom_equal(
+ "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"hidden\" name=\"_method\" value=\"delete\" /><input type=\"submit\" value=\"Hello\" /></div></form>",
+ button_to("Hello", "http://www.example.com", :method => :delete)
+ )
+ end
+
+ def test_button_to_with_method_get
+ assert_dom_equal(
+ "<form method=\"get\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
+ button_to("Hello", "http://www.example.com", :method => :get)
+ )
+ end
def test_link_tag_with_straight_url
assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com")

0 comments on commit a769b88

Please sign in to comment.