微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

原文地址:https://www.jb51.cc/ruby/274028.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐