Changeset 4370
- Timestamp:
- 05/28/06 00:33:53 (3 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/url_helper.rb (modified) (4 diffs)
- trunk/actionpack/test/template/url_helper_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r4361 r4370 1 1 *SVN* 2 3 * 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] 2 4 3 5 * follow_redirect doesn't complain about being redirected to the same controller. #5153 [dymo@mk.ukrtelecom.ua] trunk/actionpack/lib/action_view/helpers/url_helper.rb
r4014 r4370 39 39 # Javascript form. 40 40 # 41 # And a third for making the link do a POST request (instead of the regular GET) through a dynamically added form element that 42 # is instantly submitted. Note that if the user has turned off Javascript, the request will fall back on the GET. So its 43 # your responsibility to determine what the action should be once it arrives at the controller. The POST form is turned on by 44 # passing :post as true. Note, it's not possible to use POST requests and popup targets at the same time (an exception will be thrown). 41 # And a third for making the link do a non-GET request through a dynamically added form element that is instantly submitted. 42 # Note that if the user has turned off Javascript, the request will fall back on the GET. So its 43 # your responsibility to determine what the action should be once it arrives at the controller. The form is turned on by 44 # passing :method with the option of either :post, :delete, or :put as the value. Usually only :post or :delete will make sense, though. 45 # Note, it's not possible to use method request and popup targets at the same time (an exception will be thrown). 45 46 # 46 47 # Examples: … … 48 49 # link_to "Help", { :action => "help" }, :popup => true 49 50 # link_to "Busy loop", { :action => "busy" }, :popup => ['new_window', 'height=300,width=600'] 50 # link_to "Destroy account", { :action => "destroy" }, :confirm => "Are you sure?", : post => true51 # link_to "Destroy account", { :action => "destroy" }, :confirm => "Are you sure?", :method => :delete 51 52 def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference) 52 53 if html_options … … 250 251 private 251 252 def convert_options_to_javascript!(html_options) 252 confirm, popup, post = html_options.delete("confirm"), html_options.delete("popup"), html_options.delete("post") 253 confirm, popup = html_options.delete("confirm"), html_options.delete("popup") 254 255 # post is deprecated, but if its specified and method is not, assume that method = :post 256 method, post = html_options.delete("method"), html_options.delete("post") 257 method = :post if !method && post 253 258 254 259 html_options["onclick"] = case 255 when popup && post260 when popup && method 256 261 raise ActionView::ActionViewError, "You can't use :popup and :post in the same link" 257 262 when confirm && popup 258 263 "if (#{confirm_javascript_function(confirm)}) { #{popup_javascript_function(popup)} };return false;" 259 when confirm && post260 "if (#{confirm_javascript_function(confirm)}) { #{ post_javascript_function} };return false;"264 when confirm && method 265 "if (#{confirm_javascript_function(confirm)}) { #{method_javascript_function(method)} };return false;" 261 266 when confirm 262 267 "return #{confirm_javascript_function(confirm)};" 263 when post264 "#{ post_javascript_function}return false;"268 when method 269 "#{method_javascript_function(method)}return false;" 265 270 when popup 266 271 popup_javascript_function(popup) + 'return false;' … … 278 283 end 279 284 280 def post_javascript_function 281 "var f = document.createElement('form'); this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();" 285 def method_javascript_function(method) 286 submit_function = 287 "var f = document.createElement('form'); f.style.display = 'none'; " + 288 "this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;" 289 290 unless method == :post 291 submit_function << "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); " 292 submit_function << "m.setAttribute('name', '_method'); m.setAttribute('value', '#{method}'); f.appendChild(m);" 293 end 294 295 submit_function << "f.submit();" 282 296 end 283 297 trunk/actionpack/test/template/url_helper_test.rb
r4014 r4370 128 128 def test_link_tag_using_post_javascript 129 129 assert_dom_equal( 130 "<a href= \"http://www.example.com\" onclick=\"var f = document.createElement('form'); this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>",130 "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>", 131 131 link_to("Hello", "http://www.example.com", :post => true) 132 132 ) 133 133 end 134 135 def test_link_tag_using_delete_javascript 136 assert_dom_equal( 137 "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;\">Destroy</a>", 138 link_to("Destroy", "http://www.example.com", :method => :delete) 139 ) 140 end 134 141 135 142 def test_link_tag_using_post_javascript_and_confirm 136 143 assert_dom_equal( 137 "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit(); };return false;\">Hello</a>",144 "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit(); };return false;\">Hello</a>", 138 145 link_to("Hello", "http://www.example.com", :post => true, :confirm => "Are you serious?") 139 146 )