提交 84c8b2fe 编写于 作者: J José Valim

Merge pull request #3272 from odorcicd/fix_json_error_root

JSON responder should return errors with :errors root
......@@ -253,7 +253,7 @@ def display(resource, given_options={})
end
def display_errors
controller.render format => resource.errors, :status => :unprocessable_entity
controller.render format => resource_errors, :status => :unprocessable_entity
end
# Check whether the resource has errors.
......@@ -286,5 +286,13 @@ def empty_resource
def empty_json_resource
"{}"
end
def resource_errors
respond_to?("#{format}_resource_errors") ? send("#{format}_resource_errors") : resource.errors
end
def json_resource_errors
{:errors => resource.errors}
end
end
end
......@@ -745,6 +745,20 @@ def test_using_resource_for_post_with_xml_yields_unprocessable_entity_on_failure
end
end
def test_using_resource_for_post_with_json_yields_unprocessable_entity_on_failure
with_test_route_set do
@request.accept = "application/json"
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
post :using_resource
assert_equal "application/json", @response.content_type
assert_equal 422, @response.status
errors = {:errors => errors}
assert_equal errors.to_json, @response.body
assert_nil @response.location
end
end
def test_using_resource_for_put_with_html_redirects_on_success
with_test_route_set do
put :using_resource
......@@ -808,6 +822,18 @@ def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
assert_nil @response.location
end
def test_using_resource_for_put_with_json_yields_unprocessable_entity_on_failure
@request.accept = "application/json"
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
put :using_resource
assert_equal "application/json", @response.content_type
assert_equal 422, @response.status
errors = {:errors => errors}
assert_equal errors.to_json, @response.body
assert_nil @response.location
end
def test_using_resource_for_delete_with_html_redirects_on_success
with_test_route_set do
Customer.any_instance.stubs(:destroyed?).returns(true)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册