Permalink
Browse files

only try to display an api template in responders if the request is a…

… get or there are no errors

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 9766997 commit 48404a751d7cab1556c390a5915c90947d56b46e @joshk joshk committed with josevalim Mar 31, 2011
View
13 actionpack/lib/action_controller/metal/responder.rb
@@ -131,7 +131,11 @@ def to_html
# responds to :to_format and display it.
#
def to_format
- default_render
+ if get? || !has_errors?
+ default_render
+ else
+ display_errors
+ end
rescue ActionView::MissingTemplate => e
api_behavior(e)
end
@@ -155,9 +159,6 @@ def api_behavior(error)
if get?
display resource
- elsif has_errors?
- # bypass the options merging of display
- controller.render format => resource.errors, :status => :unprocessable_entity
elsif post?
display resource, :status => :created, :location => api_location
elsif has_empty_resource_definition?
@@ -210,6 +211,10 @@ def display(resource, given_options={})
controller.render given_options.merge!(options).merge!(format => resource)
end
+ def display_errors
+ controller.render format => resource.errors, :status => :unprocessable_entity
+ end
+
# Check whether the resource has errors.
#
def has_errors?
View
21 actionpack/test/controller/mime_responds_test.rb
@@ -558,6 +558,10 @@ def using_resource_with_status_and_location
respond_with(resource, :location => "http://test.host/", :status => :created)
end
+ def using_invalid_resource_with_template
+ respond_with(resource)
+ end
+
def using_resource_with_responder
responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
respond_with(resource, :responder => responder)
@@ -970,6 +974,23 @@ def test_using_resource_with_status_and_location_with_invalid_resource
assert_equal nil, @response.location
end
+ def test_using_invalid_resource_with_template
+ errors = { :name => :invalid }
+ Customer.any_instance.stubs(:errors).returns(errors)
+
+ @request.accept = "text/xml"
+
+ post :using_invalid_resource_with_template
+ assert_equal errors.to_xml, @response.body
+ assert_equal 422, @response.status
+ assert_equal nil, @response.location
+
+ put :using_invalid_resource_with_template
+ assert_equal errors.to_xml, @response.body
+ assert_equal 422, @response.status
+ assert_equal nil, @response.location
+ end
+
def test_using_resource_with_responder
get :using_resource_with_responder
assert_equal "Resource name is david", @response.body
View
1 actionpack/test/fixtures/respond_with/using_invalid_resource_with_template.xml.erb
@@ -0,0 +1 @@
+<content>I should not be displayed</content>

0 comments on commit 48404a7

Please sign in to comment.