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?
The issue is you're passing in
@check_inas the first argument of therendermethod when it expects the first argument to be a hash of options, including thestatusoption.Your
status: 201option is being passed in as a hash to the methods second argument and being ignored.Typicallya render call will look something like:
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.