1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Return a valid empty JSON on successful PUT and DELETE requests. [#5199 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Szymon Nowak 2010-10-11 19:37:03 +02:00 committed by José Valim
parent 582a088ba7
commit 0d33332571
2 changed files with 39 additions and 0 deletions

View file

@ -161,6 +161,8 @@ module ActionController #:nodoc:
display resource.errors, :status => :unprocessable_entity display resource.errors, :status => :unprocessable_entity
elsif post? elsif post?
display resource, :status => :created, :location => api_location display resource, :status => :created, :location => api_location
elsif has_empty_resource_definition?
display empty_resource, :status => :ok
else else
head :ok head :ok
end end
@ -221,5 +223,23 @@ module ActionController #:nodoc:
def default_action def default_action
@action ||= ACTIONS_FOR_VERBS[request.request_method_symbol] @action ||= ACTIONS_FOR_VERBS[request.request_method_symbol]
end end
# Check whether resource needs a specific definition of empty resource to be valid
#
def has_empty_resource_definition?
respond_to?("empty_#{format}_resource")
end
# Delegate to proper empty resource method
#
def empty_resource
send("empty_#{format}_resource")
end
# Return a valid empty JSON resource
#
def empty_json_resource
"{}"
end
end end
end end

View file

@ -709,6 +709,15 @@ class RespondWithControllerTest < ActionController::TestCase
assert_equal " ", @response.body assert_equal " ", @response.body
end end
def test_using_resource_for_put_with_json_yields_ok_on_success
Customer.any_instance.stubs(:to_json).returns('{"name": "David"}')
@request.accept = "application/json"
put :using_resource
assert_equal "application/json", @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 def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
@request.accept = "application/xml" @request.accept = "application/xml"
errors = { :name => :invalid } errors = { :name => :invalid }
@ -739,6 +748,16 @@ class RespondWithControllerTest < ActionController::TestCase
assert_equal " ", @response.body assert_equal " ", @response.body
end end
def test_using_resource_for_delete_with_json_yields_ok_on_success
Customer.any_instance.stubs(:to_json).returns('{"name": "David"}')
Customer.any_instance.stubs(:destroyed?).returns(true)
@request.accept = "application/json"
delete :using_resource
assert_equal "application/json", @response.content_type
assert_equal 200, @response.status
assert_equal "{}", @response.body
end
def test_using_resource_for_delete_with_html_redirects_on_failure def test_using_resource_for_delete_with_html_redirects_on_failure
with_test_route_set do with_test_route_set do
errors = { :name => :invalid } errors = { :name => :invalid }