在Rails 3中返回空JSON响应的首选方法是什么?

Mar*_*ain 20 rest json ruby-on-rails http

当用户将JSON发送到Rails 3应用程序中的/ update/action时,最好的响应方式是什么?

我想发送一个带有200代码的空JSON响应,类似于

head :no_content
Run Code Online (Sandbox Code Playgroud)

要么

render :nothing => true, :status => 204
Run Code Online (Sandbox Code Playgroud)

(示例来自如何在Rails控制器中返回HTTP 204).

通常我一直这样做:

render :json => {}
Run Code Online (Sandbox Code Playgroud)

要么

render :json => 'ok'
Run Code Online (Sandbox Code Playgroud)

是否有更喜欢或更多的Rails-y方式?

Sni*_*ips 28

我的Rails 3应用程序使用此类代码进行更新.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
Run Code Online (Sandbox Code Playgroud)

工作完美,这是最重要的.