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

Correctly handle the case of an API response that returns a hash by treating a single hash argument as the resource instead of as options.

This commit is contained in:
Chris Eppstein 2010-11-05 10:28:35 +08:00 committed by José Valim
parent 06d518a323
commit b1667c7c2c
2 changed files with 17 additions and 1 deletions

View file

@ -227,7 +227,7 @@ module ActionController #:nodoc:
"controller responds to in the class level" if self.class.mimes_for_respond_to.empty?
if response = retrieve_response_from_mimes(&block)
options = resources.extract_options!
options = resources.size == 1 ? {} : resources.extract_options!
options.merge!(:default_response => response)
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end

View file

@ -487,6 +487,10 @@ class RespondWithController < ActionController::Base
respond_with(resource)
end
def using_hash_resource
respond_with({:result => resource})
end
def using_resource_with_block
respond_with(resource) do |format|
format.csv { render :text => "CSV" }
@ -587,6 +591,18 @@ class RespondWithControllerTest < ActionController::TestCase
end
end
def test_using_hash_resource
@request.accept = "application/xml"
get :using_hash_resource
assert_equal "application/xml", @response.content_type
assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <name>david</name>\n</hash>\n", @response.body
@request.accept = "application/json"
get :using_hash_resource
assert_equal "application/json", @response.content_type
assert_equal %Q[{"result":["david",13]}], @response.body
end
def test_using_resource_with_block
@request.accept = "*/*"
get :using_resource_with_block