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

haj*_*poj 14 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标头:"

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

Sam*_*ron 18

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

 render :xml => post.to_xml, :status => :created, :location => post_url(post)
Run Code Online (Sandbox Code Playgroud)

告诉收件人该帖子的XML文件已创建,您将从中获取此信息post_url(post).因此,去那里;)

render方法通过Location在响应对象中设置选项来完成此操作

... ... ... 
if location = options[:location]
    response.headers["Location"] = url_for(location)
end
... ... ... 
Run Code Online (Sandbox Code Playgroud)

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