Ruby-on-rails `:location => ...` 和 `head :ok` 在 'respond_to' 格式语句中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5213956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does `:location => ...` and `head :ok` mean in the 'respond_to' format statement?
提问by user502052
I am using Ruby on Rails 3 and I would like to know what the :location => ...and head :okstatements mean in following code, how they work and how I can\should use those.
我正在使用 Ruby on Rails 3,我想知道下面代码中的:location => ...andhead :ok语句是什么意思,它们是如何工作的以及我应该如何使用它们。
respond_to do |format|
format.xml { render :xml => @user, :status => :created, :location => @user }
end
respond_to do |format|
format.xml { head :ok }
end
回答by hoha
render ... :location => @userwill set the HTTP location headerto inform the client of the location of the newly created resource (that is, its URL)head :oksetsrenderto return an empty response (so just the header, no body) with status 200.head :okis shorthand forrender nothing: true, status: :ok.
Here's a list of all the:statusoptions you can use for setting the appropriate status code.
render ... :location => @user将设置HTTP 位置标头以通知客户端新创建的资源的位置(即其 URL)head :ok设置render返回状态为 200 的空响应(因此只有标题,没有正文)。head :ok是 . 的简写render nothing: true, status: :ok。
以下是:status可用于设置适当状态代码的所有选项的列表。

