为什么 Rails 要返回“head :no_content"?对于 JSON PUT 请求? [英] Why does Rails want to return "head :no_content" for JSON PUT requests?
问题描述
我跑后rails 生成脚手架用户
Rails 3.2.11 中生成的用于更新用户的控制器函数如下所示:
After I run
rails 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
我很好奇的行是返回 head :no_content
以获取成功的 JSON 更新请求.我已经做了一些谷歌搜索,因为我猜测这是某种 RESTful 属性,不返回更新的对象,但我找不到任何声称是这种情况的东西.
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.
为什么这是默认设置,而不是在更新后返回用户对象的 JSON 表示?
Why is this the default, versus returning the JSON representation of the User object post-update?
推荐答案
好问题,显然目的是返回一个空体的 HTTP 状态码 200,见 这个讨论.也许是为了简洁或安全目的.head :no_content
似乎创建了一个空体的 HTTP 响应 200(成功),返回这个响应头:
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
另见这个相关问题.
这篇关于为什么 Rails 要返回“head :no_content"?对于 JSON PUT 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!