如何使用JBuilder Gem发送HTTP状态 [英] How to send http-status using JBuilder Gem

查看:105
本文介绍了如何使用JBuilder Gem发送HTTP状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am使用 Rails 3.0.19 JBuilder Gem 2.0.6 呈现JSON响应.

Am using Rails 3.0.19 and JBuilder Gem 2.0.6 to render JSON responses.

JBuilder: https://github.com/rails/jbuilder

JBuilder: https://github.com/rails/jbuilder

以下是用于发送特定API的错误消息的代码.

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 

由于某种原因,客户端收到200-ok状态.一会儿,我期望有400(bad_request).

For some reason, the client receives 200-ok status. While, I have expected 400 (bad_request).

有什么帮助吗?

这是我的详细代码:

  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

在before_filter方法中,我使用render_json_error_messages

And in a before_filter method, I use render_json_error_messages

推荐答案

这有效:

控制器

def some_action
  render status: :bad_request
end

some_action.jbuilder

json.something "test"

这篇关于如何使用JBuilder Gem发送HTTP状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆