After I runrails generate scaffold User
The generated controller function in Rails 3.2.11 for updating a user looks like this:
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
The line I'm curious about is returning head :no_content
for a successful JSON update request. I've done some googling, as I was guessing that this is some sort of RESTful property, to not return the updated object, but I couldn't find anything that claimed that was the case.
Why is this the default, versus returning the JSON representation of the User object post-update?
Good question, apparently the purpose is to return a HTTP status code 200 with an empty body, see this discussion. Maybe for brevity or security purposes. head :no_content
seems to create a HTTP response 200 (success) with an empty body, returning this response header:
Status Code:200 OK
see also this related question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With