Rails RABL: how to respond with specified http status code?

Alexandru Robert

Basically I have the following controller method:

  def create
    begin
      @check_in = create_check_in()
    rescue => exception
      render json: { message: exception }, status: 500
    end
  end

and the following json.rabl file:

object @check_in => :event_check_in
attributes :id

What I try to achieve is to set manually the HTTP status code of the response. It currently responds with 200, and I need it to be 201 instead.

I saw very few similar question and the answer was generally to render / respond_with from the controller action, so I tried something like this:

  def create
    begin
      @check_in = create_check_in()
      render @check_in, status: 201
    rescue => exception
      render json: { message: exception }, status: 500
    end
  end

but all my attempts failed, throwing various errors.

Is there a way I could set the status code?

melcher

The issue is you're passing in @check_in as the first argument of the render method when it expects the first argument to be a hash of options, including the status option.

Your status: 201 option is being passed in as a hash to the methods second argument and being ignored.

Typicallya render call will look something like:

render json: @check_in.as_json, status: 201
# or more commonly something like
render action: :create, status: 201 # the @check_in variable is already accessible to the view and doesn't need to be passed in
# or just
render status: 201
# since by default it will render the view with the same name as the action - which is `create`.

There are lots of ways to call render, see the docs for more.

-- EDIT -- Max has a great comment - I'd strongly advise against rescuing from all exceptions and also against doing it in a specific controller action. In addition to his suggestion, Rails 5+ supports :api formatting for exceptions out of the box or, if you need more, I'd look at a guide like this one.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

GraphQL - How to respond with different status code?

How to respond with HTTP status code in a Spring MVC @RestController @ResponseBody class returning an object?

Micronaut GraphQL: How to respond with a non-200 HTTP status code from within GraphQL handler?

Changing the response status code when using rabl

Angular Platform-Server Respond with custom http status code

How to catch an exception and respond with a status code in .NET Core

How to respond with a status code 500 to an ajax jsonp request

Rails sending HTTP status code without status message

How to make JAX-WS webservice respond with specific http code

How to get HTTP status text from HTTP status code?

Respond with a status unauthorised (401) with Rails 4

Is pythonanywhere capable to respond with a HTTP 200 status?

Respond with a custom status code in validation process

Respond with both body and status code in Nancy

How to get the status code of the http request?

How to convert HTTP status code into text in Java?

Why and how HTTP responses' status code matter?

How to register a customized http status code for flask?

How to read status code from HTTP Post?

How to handle custom HTTP status code with HttpWebResponse

how to read http status code error in component

How to Get HTTP status code with curl post

How to return different Http Status Code in ServiceStack

how to return HTTP status code in Open FaaS?

rails rabl multiple collections

How to Consume a Rails RABL API using Angular.js?

What is the best HTTP status code for blocked user profile in rails api?

HTTP status code to status message

GAE Backend- Process terminated because it failed to respond to the start request with an HTTP status code of 200-299 or 404