Rails 4 AbstractController :: Metal rendering with status! = 200 (i.e. 401, 404)

I use a simple API in my application to communicate with the Android application. I am trying to use AbstractController :: Metal mainly for performance. The problem I am facing is that the renderer ignores the status parameter that I pass.

A very simple example:

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 

Call

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

I expect to get 401, but instead get 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 

Any ideas? Overwriting response.status is the only work I have found so far, but to be honest, it looks like an ugly hack.

Thanks in advance for your understanding.

+8
ruby ruby-on-rails rack render
source share
3 answers

To do nothing and return the 401 code, use:

 render nothing: true, status: :unauthorized 
+1
source share

use head ActionController :: Head

 class Api::V1::SessionsController < Api::V1::ApiController def show head :unauthorized end end 
0
source share

I had the same problem with the below code example. He worked with ActionController::Metal in Rails 3 and started crashing after upgrading to Rails 4.2.4 :

 # config/routes.rb get :unauthorized, to: 'unauthorized#respond' # app/controllers/unauthorized_controller.rb class UnauthorizedController < ActionController::Metal def respond head :forbidden end end # spec/controllers/unauthorized_controller_spec.rb describe UnauthorizedController do it 'should respond with 403 forbidden' do get :respond, {}, {} expect(response.status).to be == 403 end end 

There was a failure after the update. To solve this problem I had to change

class UnauthorizedController < ActionController::Metal

to

class UnauthorizedController < ActionController::Base

0
source share

All Articles