Given the following controller in rails:
class AccountsController < ApplicationController
respond_to :json, :xml
def update
@account = Account.where(uuid: params[:id]).first
unless @account.nil?
if @account.update_attributes params[:account]
respond_with @account, location: account_url(@account)
else
respond_with error_hash, status: :unprocessable_entity, root: :error, location: api_account_url(@account)
end
else
respond_with error_hash, status: :not_found, root: :error, location: accounts_url
end
end
def error_hash
{ :example => "Example for this question", :parameter => 42 }
end
end
I would expect a PUT request to /accounts/update/ to do the following
- If the id exists, and the update_attributes call succeeds, deliver a
204 (No Content)success message. (I have it set to return @account, which would be nice, but no big deal. 204 is fine here.) - If the id exists, but the data is bad, deliver a
422 (Unprocessable Entity)error message, along with the xml/json to represent the error. - If the id does not exist, deliver a
404 (Not Found)error message, along with the xml/json to represent the error.
What actually happens is:
- Deliver a 204 with no body.
- Deliver a 204 with no body.
- Deliver a 204 with no body.
Why is it that it ignores both my status and my body? I've had a similar setup for GET requests that work out just fine (correct status, correct body).
Example CURL request (for an ID that does not exist):
PUT request
curl -i --header "Accept: application/xml" --header "Content-type: application/json" -X PUT -d '{"name": "whoop"}' http://localhost:3000/api/accounts/3d2cc5d06