Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 @R_874_6048@sfully 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

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

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 在Rails 3中返回空的JSON响应的首选方法是什么?全部内容,希望文章能够帮你解决ruby-on-rails – 在Rails 3中返回空的JSON响应的首选方法是什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。