From d627e1f0f5c5e5500e8f12ce7841c54ed2c37412 Mon Sep 17 00:00:00 2001 From: Antti Tarvainen Date: Sun, 23 May 2010 14:32:47 +0300 Subject: [PATCH] Responder to use the correct view path (edit.html.erb instead of new.html.erb) when a PUT request from a HTML form fails. The old implementation didn't use the method name from Rack::MethodOverride, but the original one. --- .../lib/action_controller/metal/responder.rb | 2 +- actionpack/test/controller/mime_responds_test.rb | 80 ++++++++++++++++++++ .../respond_with_integration/edit.html.erb | 1 + .../fixtures/respond_with_integration/new.html.erb | 1 + 4 files changed, 83 insertions(+), 1 deletions(-) create mode 100644 actionpack/test/fixtures/respond_with_integration/edit.html.erb create mode 100644 actionpack/test/fixtures/respond_with_integration/new.html.erb diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb index 6ad9a23..22bdcd0 100644 --- a/actionpack/lib/action_controller/metal/responder.rb +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -217,7 +217,7 @@ module ActionController #:nodoc: # the verb is POST. # def default_action - @action ||= ACTIONS_FOR_VERBS[request.method_symbol] + @action ||= ACTIONS_FOR_VERBS[request.request_method_symbol] end end end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index c8ba8bc..b12ae09 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -906,3 +906,83 @@ class MimeControllerLayoutsTest < ActionController::TestCase assert_equal '
Super iPhone
', @response.body end end + + +class RespondWithIntegrationController < ActionController::Base + respond_to :html + + def using_resource + respond_with(resource) + end + + private + def resource + Customer.new("david", request.delete? ? nil : 13) + end +end + + +class RespondWithIntegrationTest < ActionController::IntegrationTest + def test_using_resource_for_emulated_put_with_html_redirects_on_success + with_test_route_set do + post "using_resource", :_method => "put" + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "http://www.example.com/customers/13", @response.location + assert @response.redirect? + end + end + + def test_using_resource_for_emulated_put_with_html_rerender_on_failure + with_test_route_set do + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + post "using_resource", :_method => "put" + assert_equal "text/html", @response.content_type + assert_equal 200, @response.status + assert_equal "Edit world!\n", @response.body + assert_nil @response.location + end + end + + def test_using_resource_for_emulated_delete_with_html_redirects_on_success + with_test_route_set do + Customer.any_instance.stubs(:destroyed?).returns(true) + post "using_resource", :_method => "delete" + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "http://www.example.com/customers", @response.location + end + end + + def test_using_resource_for_emulated_delete_with_html_redirects_on_failure + with_test_route_set do + errors = { :name => :invalid } + Customer.any_instance.stubs(:errors).returns(errors) + Customer.any_instance.stubs(:destroyed?).returns(false) + post "using_resource", :_method => "delete" + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "http://www.example.com/customers", @response.location + end + end + + private + + def with_test_route_set + with_routing do |set| + set.draw do |map| + resources :customers + match "using_resource" => "respond_with_integration#using_resource" + end + + @app = self.class.build_app(set) do |middleware| + middleware.use Rack::MethodOverride + end + + RespondWithIntegrationController.send(:include, set.url_helpers) + + yield + end + end +end diff --git a/actionpack/test/fixtures/respond_with_integration/edit.html.erb b/actionpack/test/fixtures/respond_with_integration/edit.html.erb new file mode 100644 index 0000000..ae82dfa --- /dev/null +++ b/actionpack/test/fixtures/respond_with_integration/edit.html.erb @@ -0,0 +1 @@ +Edit world! diff --git a/actionpack/test/fixtures/respond_with_integration/new.html.erb b/actionpack/test/fixtures/respond_with_integration/new.html.erb new file mode 100644 index 0000000..96c8f1b --- /dev/null +++ b/actionpack/test/fixtures/respond_with_integration/new.html.erb @@ -0,0 +1 @@ +New world! -- 1.6.2