17

嘿,我想知道 rails 中渲染方法的位置选项是什么。此处的文档http://guides.rubyonrails.org/layouts_and_rendering.html指出:

“您可以使用 :location 选项设置 HTTP Location 标头:”

但我不知道你为什么要这样做,或者你会用它做什么。

4

2 回答 2

22

实际上location,选项用于重定向到新资源,作为处理请求的一部分。例如,

 render :xml => post.to_xml, :status => :created, :location => post_url(post)

正在告诉收件人为该帖子创建了一个 XML 文件,您将从post_url(post). 因此去那里;)

render方法通过Location在响应对象中设置选项来做到这一点

... ... ... 
if location = options[:location]
    response.headers["Location"] = url_for(location)
end
... ... ... 

Location您可以在此处 找到有关标头的详细信息http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

于 2012-08-23T04:15:21.307 回答
1

Location header用于重定向页面。

于 2012-08-23T03:56:34.500 回答