加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 20:45:09 所属栏目:百科 来源:网络整理
导读:当用户在Rails 3应用程序中将JSON发布到/ update / action时,最佳的响应方式是什么? 我想发送一个空的JSON响应与200代码,像 head :no_content 要么 render :nothing = true,:status = 204 (How to return HTTP 204 in a Rails controller的例子). 通常我一
当用户在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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读