aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-09-03 15:59:18 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-09-03 15:59:18 +0000
commita769b88819ac1ad1d89a1f9138bf1546091d3744 (patch)
tree756135df5c501075718f8529c47571268276c24c
parentf6339eb17710ce81117e06725a4403e33a934d24 (diff)
downloadrails-a769b88819ac1ad1d89a1f9138bf1546091d3744.tar.gz
rails-a769b88819ac1ad1d89a1f9138bf1546091d3744.tar.bz2
rails-a769b88819ac1ad1d89a1f9138bf1546091d3744.zip
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
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/url_helper.rb23
-rw-r--r--actionpack/test/template/url_helper_test.rb14
3 files changed, 32 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index cc1b883e5f..d6dda2d47a 100644
--- a/actionpack/CHANGELOG
+++ b/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]
diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb
index 77f217b2d4..42c28335d5 100644
--- a/actionpack/lib/action_view/helpers/url_helper.rb
+++ b/actionpack/lib/action_view/helpers/url_helper.rb
@@ -93,12 +93,14 @@ module ActionView
# 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 @@ module ActionView
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
diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb
index 4d57bc65be..77ca8094f7 100644
--- a/actionpack/test/template/url_helper_test.rb
+++ b/actionpack/test/template/url_helper_test.rb
@@ -59,6 +59,20 @@ class UrlHelperTest < Test::Unit::TestCase
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")