aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-12-02 22:47:09 -0200
committerYehuda Katz <wycats@Yehuda-Katz.local>2009-12-07 15:05:27 -0800
commit2ecfa817c95f98247d1bd7fb28b208d3f950b43f (patch)
treef2f85c7e425e94cdd3f3ca23f2d49ff762cfb8fb /actionpack
parent324fa688fcd8f5b6a8e9a0226b0cb3d2829e122c (diff)
downloadrails-2ecfa817c95f98247d1bd7fb28b208d3f950b43f.tar.gz
rails-2ecfa817c95f98247d1bd7fb28b208d3f950b43f.tar.bz2
rails-2ecfa817c95f98247d1bd7fb28b208d3f950b43f.zip
Responder redirects to resource if destroy fails.
Signed-off-by: Yehuda Katz <wycats@Yehuda-Katz.local>
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/metal/responder.rb9
-rw-r--r--actionpack/test/controller/mime_responds_test.rb42
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 "<name>david</name>", @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