Skip to content

Commit

Permalink
Added :method handling for other verbs to remote_form_tag and remote_…
Browse files Browse the repository at this point in the history
…form_for [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4374 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed May 28, 2006
1 parent d84ba8b commit 140a998
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *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] * 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] * 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]
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/form_tag_helper.rb
Expand Up @@ -20,8 +20,8 @@ module FormTagHelper
def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc) def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc)
html_options = options.stringify_keys html_options = options.stringify_keys
html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") 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 = "" method_tag = ""


case method = html_options.delete("method") case method = html_options.delete("method")
Expand Down
4 changes: 1 addition & 3 deletions actionpack/lib/action_view/helpers/prototype_helper.rb
Expand Up @@ -164,10 +164,8 @@ def form_remote_tag(options = {})


options[:html] ||= {} options[:html] ||= {}
options[:html][:onsubmit] = "#{remote_function(options)}; return false;" 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 end


# Works like form_remote_tag, but uses form_for semantics. # Works like form_remote_tag, but uses form_for semantics.
Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_view/helpers/url_helper.rb
Expand Up @@ -24,6 +24,7 @@ def url_for(options = {}, *parameters_for_method_reference)
else else
escape = true escape = true
end end

url = @controller.send(:url_for, options, *parameters_for_method_reference) url = @controller.send(:url_for, options, *parameters_for_method_reference)
escape ? html_escape(url) : url escape ? html_escape(url) : url
end end
Expand Down Expand Up @@ -57,6 +58,7 @@ def link_to(name, options = {}, html_options = nil, *parameters_for_method_refer
else else
tag_options = nil tag_options = nil
end end

url = options.is_a?(String) ? options : self.url_for(options, *parameters_for_method_reference) url = options.is_a?(String) ? options : self.url_for(options, *parameters_for_method_reference)
"<a href=\"#{url}\"#{tag_options}>#{name || url}</a>" "<a href=\"#{url}\"#{tag_options}>#{name || url}</a>"
end end
Expand Down
20 changes: 15 additions & 5 deletions actionpack/test/template/prototype_helper_test.rb
Expand Up @@ -8,17 +8,22 @@ module BaseTest
include ActionView::Helpers::UrlHelper include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper include ActionView::Helpers::TextHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormHelper include ActionView::Helpers::FormHelper
include ActionView::Helpers::CaptureHelper include ActionView::Helpers::CaptureHelper


def setup def setup
@controller = Class.new do @controller = Class.new do
def url_for(options, *parameters_for_method_reference) def url_for(options, *parameters_for_method_reference)
url = "http://www.example.com/" if options.is_a?(String)
url << options[:action].to_s if options and options[:action] options
url << "?a=#{options[:a]}" if options && options[:a] else
url << "&b=#{options[:b]}" if options && options[:a] && options[:b] url = "http://www.example.com/"
url 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
end.new end.new
end end
Expand Down Expand Up @@ -61,6 +66,11 @@ def test_form_remote_tag
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">), assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast }) form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast })
end end

def test_form_remote_tag_with_method
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><input name='_method' type='hidden' value='put' />),
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put })
end


def test_on_callbacks def test_on_callbacks
callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure] callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure]
Expand Down

0 comments on commit 140a998

Please sign in to comment.