我正在测试以下方法:
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
行产生错误undefined method `json' for #Api::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
最佳答案
如果你真的不想渲染任何东西:
head :ok # or any another status, e.g. :created, :accepted, etc
作为参数,您可以添加状态代码或符号状态( statuses 或仅在控制台中
Rack::Utils::SYMBOL_TO_STATUS_CODE
)如果您想发送 JSON 消息作为响应:
render json: { message: "removed" }, status: :ok
关于ruby-on-rails - 如何在 json 中不渲染任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970452/