In the examples given in ActiveResource Rails API, when ActiveResource does a PUT (e.g. update) to a remote RESTful rails controller, the rails controller is supposed to return a HTTP code of 204 (No Content).
But if the remote rails controller actually does that:
head :no_content #204
The Http response that ActiveResource receives is a Net::HTTPNoContent, with no body, but its Content-Length is, for some reason, “1″ !! So, ActiveResource will die in load_attributes_from_response(), when trying to call response.body.strip, but response.body is nil.
If you use Rails generated scaffolding for the remote rails controller though, the ‘update’ action that handles PUT request actually returns a 200 HTTP response:
head :ok
And in this case, everything is good.
I didn’t delve enough in ActionController and the ‘net/http’ stdlib to find out exactly why. I did notice that in net/http.rb:
class HTTPOK < HTTPSuccess # 200
HAS_BODY = true
end
class HTTPNoContent < HTTPSuccess # 204
HAS_BODY = false
end
I think ActiveResource should handle this weird behaviour of ‘net/http’ by doing a simple nil check of response.body in load_attributes_from_response(). I submitted a patch to
http://dev.rubyonrails.org/ticket/11066, so for the interested reader, you can verify my patch (that it works), or give suggestions to a better way of fixing this?
