From 2ecfa817c95f98247d1bd7fb28b208d3f950b43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 2 Dec 2009 22:47:09 -0200 Subject: Responder redirects to resource if destroy fails. Signed-off-by: Yehuda Katz --- .../lib/action_controller/metal/responder.rb | 9 +++-- actionpack/test/controller/mime_responds_test.rb | 42 +++++++++++++++++----- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb index 6c76c57839..cb0e600871 100644 --- a/actionpack/lib/action_controller/metal/responder.rb +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -80,6 +80,11 @@ module ActionController #:nodoc: class Responder attr_reader :controller, :request, :format, :resource, :resources, :options + ACTIONS_FOR_VERBS = { + :post => :new, + :put => :edit + } + def initialize(controller, resources, options={}) @controller = controller @request = controller.request @@ -138,7 +143,7 @@ module ActionController #:nodoc: def navigation_behavior(error) if get? raise error - elsif has_errors? + elsif has_errors? && default_action render :action => default_action else redirect_to resource_location @@ -209,7 +214,7 @@ module ActionController #:nodoc: # the verb is post. # def default_action - @action || (request.post? ? :new : :edit) + @action ||= ACTIONS_FOR_VERBS[request.method] end end end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index c1fa74b8c8..c857d85c39 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -599,14 +599,18 @@ class RespondWithControllerTest < ActionController::TestCase end end - def test_using_resource_for_post_with_html + def test_using_resource_for_post_with_html_redirects_on_success with_test_route_set do post :using_resource 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_post_with_html_rerender_on_failure + with_test_route_set do errors = { :name => :invalid } Customer.any_instance.stubs(:errors).returns(errors) post :using_resource @@ -617,16 +621,20 @@ class RespondWithControllerTest < ActionController::TestCase end end - def test_using_resource_for_post_with_xml + def test_using_resource_for_post_with_xml_yields_created_on_success with_test_route_set do @request.accept = "application/xml" - post :using_resource assert_equal "application/xml", @response.content_type assert_equal 201, @response.status assert_equal "david", @response.body assert_equal "http://www.example.com/customers/13", @response.location + end + end + def test_using_resource_for_post_with_xml_yields_unprocessable_entity_on_failure + with_test_route_set do + @request.accept = "application/xml" errors = { :name => :invalid } Customer.any_instance.stubs(:errors).returns(errors) post :using_resource @@ -637,14 +645,18 @@ class RespondWithControllerTest < ActionController::TestCase end end - def test_using_resource_for_put_with_html + def test_using_resource_for_put_with_html_redirects_on_success with_test_route_set do put :using_resource 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_put_with_html_rerender_on_failure + with_test_route_set do errors = { :name => :invalid } Customer.any_instance.stubs(:errors).returns(errors) put :using_resource @@ -655,14 +667,16 @@ class RespondWithControllerTest < ActionController::TestCase end end - def test_using_resource_for_put_with_xml + def test_using_resource_for_put_with_xml_yields_ok_on_success @request.accept = "application/xml" - put :using_resource assert_equal "application/xml", @response.content_type assert_equal 200, @response.status assert_equal " ", @response.body + end + def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure + @request.accept = "application/xml" errors = { :name => :invalid } Customer.any_instance.stubs(:errors).returns(errors) put :using_resource @@ -672,7 +686,7 @@ class RespondWithControllerTest < ActionController::TestCase assert_nil @response.location end - def test_using_resource_for_delete_with_html + def test_using_resource_for_delete_with_html_redirects_on_success with_test_route_set do Customer.any_instance.stubs(:destroyed?).returns(true) delete :using_resource @@ -682,7 +696,7 @@ class RespondWithControllerTest < ActionController::TestCase end end - def test_using_resource_for_delete_with_xml + def test_using_resource_for_delete_with_xml_yields_ok_on_success Customer.any_instance.stubs(:destroyed?).returns(true) @request.accept = "application/xml" delete :using_resource @@ -691,6 +705,18 @@ class RespondWithControllerTest < ActionController::TestCase assert_equal " ", @response.body end + def test_using_resource_for_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) + delete :using_resource + assert_equal "text/html", @response.content_type + assert_equal 302, @response.status + assert_equal "http://www.example.com/customers/13", @response.location + end + end + def test_using_resource_with_parent_for_get @request.accept = "application/xml" get :using_resource_with_parent -- cgit v1.2.3