Rails 4 AbstractController ::具有状态的金属渲染!= 200(即401,404)

时间:2014-08-27 15:40:17

标签: ruby-on-rails ruby render rack

我在我的应用程序中实现了一个简单的API来与Android应用程序进行通信。我尝试使用AbstractController :: Metal主要是为了提高性能。我遇到的问题是渲染忽略了我传递的状态选项。

很简单的例子:

class Api::V1::ApiController < ActionController::Metal
  include AbstractController::Rendering
  include ActionController::Renderers::All
  include ActionController::RackDelegation
  include ActionController::MimeResponds
end

class Api::V1::SessionsController < Api::V1::ApiController
  def show
    render status: :unauthorized   # using 401 yields the same result
  end
end

致电

curl -v -X GET http://app.dev:3000/api/v1/sessions.json

我希望收到401,但我得到200 OK:

> GET /api/v1/sessions.json HTTP/1.1
> User-Agent: curl/7.30.0
> Host: app.dev:3000
> Accept: */*
> 
< HTTP/1.1 200 OK

有什么想法吗?覆盖response.status是迄今为止我发现的唯一一项工作,但老实说它看起来像一个丑陋的黑客。

提前感谢您的见解。

2 个答案:

答案 0 :(得分:1)

要渲染任何内容并返回代码401,请使用:

render nothing: true, status: :unauthorized

答案 1 :(得分:0)

使用head DRY

class Api::V1::SessionsController < Api::V1::ApiController
  def show
    head :unauthorized
  end
end
相关问题