提交 a769b888 编写于 作者: J Jeremy Kemper

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
上级 f6339eb1
*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]
......
......@@ -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>
......@@ -114,6 +116,13 @@ 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
......@@ -123,8 +132,8 @@ def button_to(name, options = {}, html_options = nil)
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
......
......@@ -60,6 +60,20 @@ def test_button_to_enabled_disabled
)
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")
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册