From 140a998de31ed4547e0d21715165f4b338030673 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 28 May 2006 23:39:37 +0000 Subject: [PATCH] Added :method handling for other verbs to remote_form_tag and remote_form_for [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4374 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ .../action_view/helpers/form_tag_helper.rb | 4 ++-- .../action_view/helpers/prototype_helper.rb | 4 +--- .../lib/action_view/helpers/url_helper.rb | 2 ++ .../test/template/prototype_helper_test.rb | 20 ++++++++++++++----- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 1329927893..bf68e40466 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added :method handling for other verbs to remote_form_tag and remote_form_for [DHH] + * 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] * 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] diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 0db1977051..816f1064f7 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -20,8 +20,8 @@ module ActionView def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc) html_options = options.stringify_keys html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") - html_options["action"] = url_for(url_for_options, *parameters_for_url) - + html_options["action"] = url_for(url_for_options, *parameters_for_url) + method_tag = "" case method = html_options.delete("method") diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 3c4c0ba98d..bf32dd9f49 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -164,10 +164,8 @@ module ActionView options[:html] ||= {} options[:html][:onsubmit] = "#{remote_function(options)}; return false;" - options[:html][:action] = options[:html][:action] || url_for(options[:url]) - options[:html][:method] = options[:html][:method] || "post" - tag("form", options[:html], true) + form_tag(options[:html].delete(:action) || url_for(options[:url]), options[:html]) end # Works like form_remote_tag, but uses form_for semantics. diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index df728fd655..77f217b2d4 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -24,6 +24,7 @@ module ActionView else escape = true end + url = @controller.send(:url_for, options, *parameters_for_method_reference) escape ? html_escape(url) : url end @@ -57,6 +58,7 @@ module ActionView else tag_options = nil end + url = options.is_a?(String) ? options : self.url_for(options, *parameters_for_method_reference) "#{name || url}" end diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index ceeb418e34..56b1ed263c 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -8,17 +8,22 @@ module BaseTest include ActionView::Helpers::UrlHelper include ActionView::Helpers::TagHelper include ActionView::Helpers::TextHelper + include ActionView::Helpers::FormTagHelper include ActionView::Helpers::FormHelper include ActionView::Helpers::CaptureHelper def setup @controller = Class.new do def url_for(options, *parameters_for_method_reference) - url = "http://www.example.com/" - url << options[:action].to_s if options and options[:action] - url << "?a=#{options[:a]}" if options && options[:a] - url << "&b=#{options[:b]}" if options && options[:a] && options[:b] - url + if options.is_a?(String) + options + else + url = "http://www.example.com/" + url << options[:action].to_s if options and options[:action] + url << "?a=#{options[:a]}" if options && options[:a] + url << "&b=#{options[:b]}" if options && options[:a] && options[:b] + url + end end end.new end @@ -61,6 +66,11 @@ class PrototypeHelperTest < Test::Unit::TestCase assert_dom_equal %(
), form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast }) end + + def test_form_remote_tag_with_method + assert_dom_equal %(), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put }) + end def test_on_callbacks callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure]