2016-01-23 83 views
3

我测试下面的方法:如何在json中不渲染任何东西?

def destroy 
    if @article.destroy 
     render json nothing: true, message: "removed", status: :ok 
    else 
     render json: @article, message: "Failed to remove", status: :bad_request 
    end 
    end 

render json nothing线生成错误为#API

未定义的方法`JSON” :: V1 :: ArticlesController:0x000000074f6148

将行更改为render json: message: "removed", status: :ok没有区别。如何不渲染?

更新:我试了下面的代码,删除后回应No response received,而我期望的消息。

def destroy 
    if @article.destroy 
     respond_to do |format| 
     format.json { render nothing: true, message: "removed", status: :ok } 
     end 
    else 
     render json: @article, message: "Failed to remove", status: :bad_request 
    end 
    end 

回答

8

如果你真的想不呈现任何内容:

head :ok 

如果您想发送一个JSON消息作为回应:

render json: { message: "removed" }, status: :ok 
+1

谢谢,有也只能以此为JSON的方法吗? – Marty

+0

它是有道理的,但对我来说,这呈现:'没有收到任何答复'(它已删除文章)。我希望看到这个消息。 – Marty

+0

为了确保我做得正确,我添加了用于OP的代码。 – Marty

4

返回HTTP 204无内容将使如果你不想返回任何东西,那更有意义。

render :nothing => true, :status => 204 

或者更好的只是:

head :no_content 
+0

谢谢,它的工作原理。我想这是不可能在同一时间显示一条消息(在序列化程序中包含属性)? – Marty

相关问题