本文介绍了什么`:location => ...`和`head:ok`意思是在'respond_to'格式声明中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby on Rails 3,我想知道:location => ... head:ok 语句在下面的代码中有意义,它们是如何工作的以及如何使用这些语句。

  respond_to do | format | 
format.xml {render:xml => @user,:status => :created,:location => @user}
结束

respond_to do | format |
format.xml {head:ok}
end


解决方案

  1. render ...:location => @user 会设置来通知客户新创建的资源的位置(即其URL)



I am using Ruby on Rails 3 and I would like to know what the :location => ... and head :ok statements mean in following code, how they work and how I can\should use those.

respond_to do |format|
    format.xml  { render :xml => @user, :status => :created, :location => @user }
end

respond_to do |format|
    format.xml  { head :ok }
end
解决方案
  1. render ... :location => @user will set the HTTP location header to inform the client of the location of the newly created resource (that is, its URL)

  2. head :ok sets render to return an empty response (so just the header, no body) with status 200. head :ok is shorthand for render nothing: true, status: :ok.
    Here's a list of all the :status options you can use for setting the appropriate status code.

这篇关于什么`:location => ...`和`head:ok`意思是在'respond_to'格式声明中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:16