qua*_*ato 0 json ruby-on-rails devise
我要过去了
{"email":"invalidemail@gibberish.com"}
Run Code Online (Sandbox Code Playgroud)
作为参数.
我得到200一个零身体的回应.为什么我看不到401?
当我运行第一个
User.where('email = ?', params[:email]).first
Run Code Online (Sandbox Code Playgroud)
在控制台中查询,我得到零.我也试过密码,测试无效密码.在控制台中,它失败了.但我200在这里得到回应:
def sign_in
user = User.where('email = ?', params[:email]).first
if !user
render :json => user, status => 401
return
end
if user.valid_password?(params[:password])
render :json => user
return
end
render :json => user, status => 401
end
Run Code Online (Sandbox Code Playgroud)
虽然head在这种情况下你应该使用@baldrick是正确的,但你的问题是"我为什么看不到401?",答案是:因为你有(status而不是:status符号).
这应该工作:
render :json => user, :status => 401
Run Code Online (Sandbox Code Playgroud)
作为旁注,起初我觉得有点奇怪,因为当你调用一种(我认为)不存在的方法时,status你就没有得到NameError它.但事实上,在模块status中的Rails'中定义了它委托给默认响应对象():ActionController::BaseRackDelegation@_response
delegate :headers, :status=, :location=, :content_type=,
:status, :location, :content_type, :to => "@_response"
Run Code Online (Sandbox Code Playgroud)
因此status评估为@_response.status,(最初至少)设置为200(ok).
因此,render :json => user, status => 401相当于render :json => user, 200 => 401.该200 => 401会被忽略,因为它不匹配任何定义的选项,你会得到一个200响应代码而不是预期的401.
希望这能说明问题!