6

I have an Api controller using ActionController::Metal on Rails 4.1.6 like this:

class Api < ActionController::Metal
  include AbstractController::Rendering 
  include ActionController::ImplicitRender
  include ActionController::MimeResponds
  include ActionController::RequestForgeryProtection  
  include AbstractController::Callbacks
  include ActionController::HttpAuthentication::Token::ControllerMethods
  include ActionController::Head

  ...
end

However, if I put this in an action

render 'not_found', status: 404

It renders the 'not_found' template correctly but returns a 200 status code. Running the same render in ActionController::Base, it returns the desired 404. What module am I missing here?

Michael
  • 683
  • 9
  • 21

3 Answers3

2

try to include these 3 modules in the same order

include AbstractController::Rendering
include ActionView::Rendering
include ActionController::Rendering
Renat
  • 53
  • 1
  • 6
0

Replace this:

include AbstractController::Rendering

with this:

include ActionController::Rendering
Substantial
  • 6,684
  • 2
  • 31
  • 40
  • If I replace `AbstractController::Rendering` with `ActionController::Rendering`, it raises the following error: `NoMethodError: super: no superclass method 'render' for #`. If I include both, it renders, but without the status (the original problem). – Michael Oct 03 '14 at 12:42
  • Strange. The API doc says `ActionController::Rendering` includes `AbstractController::Rendering` so there is no need to explicitly include both. I reproduced your problem in Rails 4.0.2 and my solution solves it. This might be a bug in Rails. – Substantial Oct 03 '14 at 22:25
  • 1
    I'm on Rails 4.1.6, not sure if they made substantial changes here between versions? Maybe I need to raise the issue with the maintainers and see what they say. – Michael Oct 03 '14 at 23:36
0

Try to include these modules and you are good to respond with status code and render the response in json format.

include AbstractController::Rendering
include ActionController::Rendering
include ActionController::Renderers::All
include ActionController::Rescue

And here is the render code:

render json: { message: "You request is forbidden" }, status: :forbidden