Anonymous avatar Anonymous committed a0502dc

[svn] * button_to accepts method keyword so you can PUT and DELETE with it.
(Follows #4914 Rails changeset)

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
 0.2.1 (**svn**)
+* button_to accepts method keyword so you can PUT and DELETE with it. 
+  (Follows #4914 Rails changeset)
 * Fixed auto_link to parse more valid url formats (Thanks Jamie Wilkinson).
 * Sync'd text helper from latest Rails version.
 * Fixed form tag's method matching to be case insensitive.

tests/test_url.py

                button_to("Hello", "http://www.example.com", disabled=False))
         self.assertEqual("<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input disabled=\"disabled\" type=\"submit\" value=\"Hello\" /></div></form>",
                button_to("Hello", "http://www.example.com", disabled=True))
+    
+    def test_button_to_with_method_delete(self):
+        self.assertEqual("<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input name=\"_method\" type=\"hidden\" value=\"delete\" /><input type=\"submit\" value=\"Hello\" /></div></form>", 
+            button_to("Hello", "http://www.example.com", method='delete'))
+
+    def test_button_to_with_method_get(self):
+        self.assertEqual("<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'))
 
     def test_link_tag_with_straight_url(self):
         self.assertEqual("<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com"))

webhelpers/rails/urls.py

 """URL Helpers"""
-# Last synced with Rails copy at Revision 4374 on Aug 20th, 2006.
+# Last synced with Rails copy at Revision 4914 on Sep 4th, 2006.
 
 import cgi
 import urllib
     
     Example 2::
     
-        >> button_to("Destroy", url(action='destroy', id=3), confirm="Are you sure?")
+        >> button_to("Destroy", url(action='destroy', id=3), confirm="Are you sure?", method='delete')
         <form method="post" action="/feeds/destroy/3" class="button-to">
-        <div><input onclick="return confirm('Are you sure?');" value="Destroy" type="submit" />
+        <div>
+            <input type="hidden" name="_method" value="delete" />
+            <input onclick="return confirm('Are you sure?');" value="Destroy" type="submit" />
         </div>
         </form>
     
     if html_options:
         convert_boolean_attributes(html_options, ['disabled'])
     
+    method_tag = ''
+    method = html_options.pop('method', '')
+    if method in ['put', 'delete']:
+        method_tag = tags.tag('input', type_='hidden', name_='_method', value=method)
+    
+    form_method = (method == 'get' and 'get') or 'post'
+    
     confirm = html_options.get('confirm')
     if confirm:
         del html_options['confirm']
     
     html_options.update(dict(type='submit', value=name))
     
-    return """<form method="post" action="%s" class="button-to"><div>""" % html_escape(url) + \
-           tags.tag("input", **html_options) + "</div></form>"
+    return """<form method="%s" action="%s" class="button-to"><div>""" % \
+        (form_method, html_escape(url)) + method_tag + tags.tag("input", **html_options) + "</div></form>"
 
 def link_to_unless_current(name, url, **html_options):
     """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.