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

Changeset 4371

Show
Ignore:
Timestamp:
05/28/06 01:04:27 (2 years ago)
Author:
david
Message:

Expanded :method option in FormHelper#form_tag to allow for verbs other than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post [DHH]

Files:

Legend:

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

    r4370 r4371  
    11*SVN* 
     2 
     3* Expanded :method option in FormHelper#form_tag to allow for verbs other than GET and POST by automatically creating a hidden form field named _method, which will simulate the other verbs over post [DHH] 
    24 
    35* 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] 
  • trunk/actionpack/lib/action_view/helpers/form_tag_helper.rb

    r3361 r4371  
    1515      # Options: 
    1616      # * <tt>:multipart</tt> - If set to true, the enctype is set to "multipart/form-data". 
    17       # * <tt>:method</tt> - The method to use when submitting the form, usually either "get" or "post". 
     17      # * <tt>:method</tt>    - The method to use when submitting the form, usually either "get" or "post". 
     18      #                         If "put", "delete", or another verb is used, a hidden input with name _method  
     19      #                         is added to simulate the verb over post. 
    1820      def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc) 
    19         html_options = { "method" => "post" }.merge(options.stringify_keys) 
     21        html_options = options.stringify_keys 
    2022        html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") 
    2123        html_options["action"] = url_for(url_for_options, *parameters_for_url) 
    22         tag :form, html_options, true 
     24         
     25        method_tag = "" 
     26         
     27        case method = html_options.delete("method") 
     28          when /^get$/i # must be case-insentive, but can't use downcase as might be nil 
     29            html_options["method"] = "get" 
     30          when /^post$/i, nil 
     31            html_options["method"] = "post" 
     32          else 
     33            html_options["method"] = "post" 
     34            method_tag = tag(:input, :type => "hidden", :name => "_method", :value => method) 
     35        end 
     36         
     37        tag(:form, html_options, true) + method_tag 
    2338      end 
    2439 
  • trunk/actionpack/test/template/form_helper_test.rb

    r4334 r4371  
    231231    expected =  
    232232      "<form action='http://www.example.com' id='create-post' method='post'>" + 
     233      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
     234      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
     235      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + 
     236      "<input name='post[secret]' type='hidden' value='0' />" + 
     237      "</form>" 
     238 
     239    assert_dom_equal expected, _erbout 
     240  end 
     241 
     242  def test_form_for_with_method 
     243    _erbout = '' 
     244 
     245    form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f| 
     246      _erbout.concat f.text_field(:title) 
     247      _erbout.concat f.text_area(:body) 
     248      _erbout.concat f.check_box(:secret) 
     249    end 
     250 
     251    expected =  
     252      "<form action='http://www.example.com' id='create-post' method='post'>" + 
     253      "<input name='_method' type='hidden' value='put' />" + 
    233254      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + 
    234255      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + 
  • trunk/actionpack/test/template/form_tag_helper_test.rb

    r3361 r4371  
    3131    actual = form_tag({}, { 'multipart' => true }) 
    3232    expected = %(<form action="http://www.example.com" enctype="multipart/form-data" method="post">) 
     33    assert_dom_equal expected, actual 
     34  end 
     35 
     36  def test_form_tag_with_method 
     37    actual = form_tag({}, { :method => :put }) 
     38    expected = %(<form action="http://www.example.com" method="post"><input type="hidden" name="_method" value="put" />) 
    3339    assert_dom_equal expected, actual 
    3440  end