给定 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
我希望PUT/accounts/update/ 请求执行以下操作
- 如果 id 存在,并且 update_attributes 调用成功,则传递
204 (No Content)成功消息。(我将它设置为返回@account,这很好,但没什么大不了的。204 在这里很好。) - 如果 id 存在,但数据不正确,则传递
422 (Unprocessable Entity)错误消息,以及表示错误的 xml/json。 - 如果 id 不存在,则传递
404 (Not Found)错误消息,以及表示错误的 xml/json。
实际发生的是:
- 交付没有主体的 204。
- 交付没有主体的 204。
- 交付没有主体的 204。
为什么它既无视我的地位,也无视我的身体?GET对于效果很好的请求(正确的状态,正确的正文),我有一个类似的设置。
示例CURL请求(针对不存在的 ID):
PUT要求
curl -i --header "Accept: application/xml" --header "Content-type: application/json" -X PUT -d '{"name": "whoop"}' http://localhost:3000/api /accounts/3d2cc5d0653911e2aaadc82a14ffee9
HTTP/1.1 204 无内容
位置:http://localhost:3000/api/accounts
X-Ua 兼容:IE=Edge
缓存控制:无缓存
X 请求 ID:bf0a02f452fbace65576aab6d2bd7c1e
X 运行时:0.029193
服务器:WEBrick/1.3.1 (Ruby/1.9.3/2013-01-15)
日期:2013 年 1 月 24 日星期四 08:01:31 GMT
连接:关闭
设置 Cookie:_bankshare_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJWFmNmI2MmU0MzViMmE3N2YzMDIzOTdjMDJmZDhiMzEwBjsAVA%3D%3D--133e394eb760a7fce07f1fd51349dc46c2d51626;路径=/; HttpOnly
GET要求
curl -i --header "Accept: application/json" --header "Content-type: application/json" -X GET http://localhost:3000/api/accounts/3d2cc5d0653911e2aaadc82a14ffee9
HTTP/1.1 404 未找到
内容类型:应用程序/json;字符集=utf-8
X-Ua 兼容:IE=Edge
缓存控制:无缓存
X 请求 ID:9cc0d1cdfb27bb86a206cbc38cd75473
X 运行时:0.005118
服务器:WEBrick/1.3.1 (Ruby/1.9.3/2013-01-15)
日期:2013 年 1 月 24 日星期四 08:19:45 GMT
内容长度:116
连接:保持活动
{"friendly-status":"not-found","status":404,"message":"找不到 ID 为 '3d2cc5d0653911e2aaadc82a14ffee9' 的帐户"}