Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4370

Show
Ignore:
Timestamp:
05/28/06 00:33:53 (3 years ago)
Author:
david
Message:

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]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r4361 r4370  
    11*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] 
    24 
    35* 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  
    3939      # Javascript form. 
    4040      # 
    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). 
    4546      # 
    4647      # Examples: 
     
    4849      #   link_to "Help", { :action => "help" }, :popup => true 
    4950      #   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 => tru
     51      #   link_to "Destroy account", { :action => "destroy" }, :confirm => "Are you sure?", :method => :delet
    5152      def link_to(name, options = {}, html_options = nil, *parameters_for_method_reference) 
    5253        if html_options 
     
    250251      private 
    251252        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 
    253258         
    254259          html_options["onclick"] = case 
    255             when popup && post 
     260            when popup && method 
    256261              raise ActionView::ActionViewError, "You can't use :popup and :post in the same link" 
    257262            when confirm && popup 
    258263              "if (#{confirm_javascript_function(confirm)}) { #{popup_javascript_function(popup)} };return false;" 
    259             when confirm && post 
    260               "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;" 
    261266            when confirm 
    262267              "return #{confirm_javascript_function(confirm)};" 
    263             when post 
    264               "#{post_javascript_function}return false;" 
     268            when method 
     269              "#{method_javascript_function(method)}return false;" 
    265270            when popup 
    266271              popup_javascript_function(popup) + 'return false;' 
     
    278283        end 
    279284         
    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();" 
    282296        end 
    283297 
  • trunk/actionpack/test/template/url_helper_test.rb

    r4014 r4370  
    128128  def test_link_tag_using_post_javascript 
    129129    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>", 
    131131      link_to("Hello", "http://www.example.com", :post => true) 
    132132    ) 
    133133  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 
    134141   
    135142  def test_link_tag_using_post_javascript_and_confirm 
    136143    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>", 
    138145      link_to("Hello", "http://www.example.com", :post => true, :confirm => "Are you serious?") 
    139146    )