我测试下面的方法:如何在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
谢谢,有也只能以此为JSON的方法吗? – Marty
它是有道理的,但对我来说,这呈现:'没有收到任何答复'(它已删除文章)。我希望看到这个消息。 – Marty
为了确保我做得正确,我添加了用于OP的代码。 – Marty