Am using Rails 3.0.19 and JBuilder Gem 2.0.6 to render JSON responses.
JBuilder: https://github.com/rails/jbuilder
Following is the code am using to send error-messages for a specific API.
render :json, :template=>"/api/shared/errors.json.jbuilder", :status=> :bad_request
For some reason, the client receives 200-ok status. While, I have expected 400 (bad_request).
Any help, please?
Here is my code in detail:
def render_json_error_messages
#render :template=> "/api/shared/errors.json.jbuilder", :status=> :bad_request, :formats => [:json]
respond_to do |format|
format.json {
render :template=> "/api/shared/errors.json.jbuilder", :status=> 400
}
end
end
And in a before_filter method, I use render_json_error_messages
maybe reverse the thinking:
api/shared/index.jbuilder
works in Rails5.1.4
This works:
controller
some_action.jbuilder
I don't know about Rails 3.0, but I was able to provide the appropriate status by simply adding a
respond_to
block to the controller action. As an example, I have ancreate
like so:The above code sets my status code to 201 and renders
app/views/orders/create.json.jbuilder
Hope that helps.
Try rendering jbuilder to string then set the status... works in Rails 4.1.4
Else following also works