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

Fel*_*och 10 ruby rack ruby-on-rails render

我正在我的应用程序中实现一个简单的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
Run Code Online (Sandbox Code Playgroud)

调用

curl -v -X GET http://app.dev:3000/api/v1/sessions.json
Run Code Online (Sandbox Code Playgroud)

我希望收到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
Run Code Online (Sandbox Code Playgroud)

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

提前感谢您的见解.

Zha*_*eng 0

使用head ActionController::Head

class Api::V1::SessionsController < Api::V1::ApiController
  def show
    head :unauthorized
  end
end
Run Code Online (Sandbox Code Playgroud)