mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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>
This commit is contained in:
parent
9766997f4c
commit
48404a751d
3 changed files with 31 additions and 4 deletions
|
@ -131,7 +131,11 @@ module ActionController #:nodoc:
|
|||
# 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 @@ module ActionController #:nodoc:
|
|||
|
||||
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 @@ module ActionController #:nodoc:
|
|||
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?
|
||||
|
|
|
@ -558,6 +558,10 @@ class RespondWithController < ActionController::Base
|
|||
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 @@ class RespondWithControllerTest < ActionController::TestCase
|
|||
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
|
||||
|
|
1
actionpack/test/fixtures/respond_with/using_invalid_resource_with_template.xml.erb
vendored
Normal file
1
actionpack/test/fixtures/respond_with/using_invalid_resource_with_template.xml.erb
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
<content>I should not be displayed</content>
|
Loading…
Reference in a new issue