ruby-on-rails – 在Rails 3中返回空的JSON响应的首选方法是什么?

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

我想发送一个空的JSON响应与200代码,像

head :no_content

要么

render :nothing => true,:status => 204

(How to return HTTP 204 in a Rails controller的例子).

通常我一直这样做:

render :json => {}

要么

render :json => 'ok'

有没有更喜欢或更多的Rails-y方式呢?

解决方法

我的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

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...