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

时间:2012-08-23 03:41:46

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

嘿,我想知道rails中render方法的位置选项是什么。这里的文档http://guides.rubyonrails.org/layouts_and_rendering.html表明:

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

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

2 个答案:

答案 0 :(得分:16)

实际上,location选项用于在处理请求时重定向到新资源。例如,

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

告诉收件人已创建帖子的XML文件,您将从post_url(post)获取此文件。因此,GO THERE;)

render方法通过在响应对象

中设置Location选项来完成此操作
... ... ... 
if location = options[:location]
    response.headers["Location"] = url_for(location)
end
... ... ... 

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

答案 1 :(得分:0)

Location header用于重定向页面。