当用户在Rails3应用程序的/update/ action中发布JSON时,最好的响应方式是什么?
我只想发送一个带有200代码的空JSON响应,类似于
head :no_content
或
render :nothing => true, :status => 204
(来自How to return HTTP 204 in a Rails controller的示例)。
通常我都是这样做的:
render :json => {}
或
render :json => 'ok'
有没有更好的或者更多的Rails-y方法呢?
发布于 2012-11-29 02:39:34
我的Rails3应用程序使用这样的代码进行更新。html和xml的代码是由Rails自动生成的,所以我只是使用相同的格式添加了JSON渲染器。
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
format.json { render :json => @product.errors, :status => :unprocessable_entity }
end
end
完美工作,这才是最重要的。
https://stackoverflow.com/questions/13584025
复制相似问题