Mar*_*rty 11 api json ruby-on-rails ruby-on-rails-4
我正在测试以下方法:
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
Run Code Online (Sandbox Code Playgroud)
该render json nothing
行生成错误
#Api :: V1 :: ArticlesController:0x000000074f6148的未定义方法`json'
改变线条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
Run Code Online (Sandbox Code Playgroud)
Ole*_*huk 20
如果你真的不想渲染任何东西:
head :ok # or any another status, e.g. :created, :accepted, etc
Run Code Online (Sandbox Code Playgroud)
如果要将JSON消息作为响应发送:
render json: { message: "removed" }, status: :ok
Run Code Online (Sandbox Code Playgroud)
如果您不想返回任何内容,则返回HTTP 204 No Content会更有意义.
render :nothing => true, :status => 204
Run Code Online (Sandbox Code Playgroud)
或者更好的是:
head :no_content
Run Code Online (Sandbox Code Playgroud)