Rails:渲染方法中的位置选项是什么



嘿,我想知道rails中render方法的位置选项是什么http://guides.rubyonrails.org/layouts_and_rendering.html状态:

"您可以使用:location选项设置HTTP location标头:"

但我不知道你为什么要这么做,也不知道你会用这个做什么。

实际上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.

Location header用于重定向页面。

最新更新