我正在测试以下方法:
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
行生成错误
未定义的方法`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
答案 0 :(得分:16)
如果你真的不想渲染任何东西:
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
答案 1 :(得分:4)
如果您不想返回任何内容,则返回 HTTP 204无内容会更有意义。
render :nothing => true, :status => 204
或者更好:
head :no_content