提交 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* *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 sanitize text helper to strip plaintext tags, and <img src="javascript:bang">. [Rick Olson]
* Update routing documentation. Closes #6017 [Nathan Witmer] * Update routing documentation. Closes #6017 [Nathan Witmer]
......
...@@ -93,12 +93,14 @@ def link_to(name, options = {}, html_options = nil, *parameters_for_method_refer ...@@ -93,12 +93,14 @@ def link_to(name, options = {}, html_options = nil, *parameters_for_method_refer
# Example 2: # Example 2:
# #
# button_to "Destroy", { :action => 'destroy', :id => 3 }, # button_to "Destroy", { :action => 'destroy', :id => 3 },
# :confirm => "Are you sure?" # :confirm => "Are you sure?", :method => :delete
# #
# Generates the following HTML (sans formatting): # Generates the following HTML (sans formatting):
# #
# <form method="post" action="/feeds/destroy/3" class="button-to"> # <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" /> # value="Destroy" type="submit" />
# </div> # </div>
# </form> # </form>
...@@ -113,18 +115,25 @@ def link_to(name, options = {}, html_options = nil, *parameters_for_method_refer ...@@ -113,18 +115,25 @@ def link_to(name, options = {}, html_options = nil, *parameters_for_method_refer
def button_to(name, options = {}, html_options = nil) def button_to(name, options = {}, html_options = nil)
html_options = (html_options || {}).stringify_keys html_options = (html_options || {}).stringify_keys
convert_boolean_attributes!(html_options, %w( disabled )) 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") if confirm = html_options.delete("confirm")
html_options["onclick"] = "return #{confirm_javascript_function(confirm)};" html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
end end
url = options.is_a?(String) ? options : url_for(options) url = options.is_a?(String) ? options : url_for(options)
name ||= url name ||= url
html_options.merge!("type" => "submit", "value" => name) html_options.merge!("type" => "submit", "value" => name)
"<form method=\"post\" action=\"#{h url}\" class=\"button-to\"><div>" + "<form method=\"#{form_method}\" action=\"#{h url}\" class=\"button-to\"><div>" +
tag("input", html_options) + "</div></form>" method_tag + tag("input", html_options) + "</div></form>"
end end
......
...@@ -59,6 +59,20 @@ def test_button_to_enabled_disabled ...@@ -59,6 +59,20 @@ def test_button_to_enabled_disabled
button_to("Hello", "http://www.example.com", :disabled => true) button_to("Hello", "http://www.example.com", :disabled => true)
) )
end 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 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") assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册