I'm implementing a simple API in my application to communicate with an Android application. I'm trying to use AbstractController::Metal mainly for performance. The problem I'm having is that render is ignoring the status option that I'm passing.
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
Calling
curl -v -X GET http://app.dev:3000/api/v1/sessions.json
I'd expect to receive a 401 but instead I get a 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 around I've found so far, but honestly it looks like an ugly hack.
Thank you in advance for your insights.
By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.
By using render json: in our Rails controller, we can take entire models or even collections of models, have Rails convert them to JSON, and send them out on request.
Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html.
To render nothing and return code 401
use:
render nothing: true, status: :unauthorized
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With