问题描述
嘿,我想知道 rails 中渲染方法的位置选项是什么.此处的文档 http://guides.rubyonrails.org/layouts_and_rendering.html 指出:
Hey I am wondering what the location option for the render method in rails is. The docs here http://guides.rubyonrails.org/layouts_and_rendering.html states:
您可以使用 :location 选项来设置 HTTP 位置标头:"
"You can use the :location option to set the HTTP Location header:"
但我不知道你为什么要这样做,或者你会用它做什么.
But I have no idea why you would do this, or what you would use this for.
推荐答案
实际上 location
选项用于重定向到新资源,作为处理请求的一部分.例如,
Actually location
option is used to redirect to a new resource as part of processing the request. For example,
render :xml => post.to_xml, :status => :created, :location => post_url(post)
告诉接收者该帖子的 XML 文件已创建,您将从 post_url(post)
获取此文件.因此去那里;)
is telling the recipient that a XML file for the post is created and you will get this from post_url(post)
. Hence GO THERE ;)
render
方法通过在响应对象中设置 Location
选项来实现这一点
render
method does this by setting the Location
option in response object
... ... ...
if location = options[:location]
response.headers["Location"] = url_for(location)
end
... ... ...
您可以在此处http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30.
这篇关于Rails:渲染方法中的位置选项是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!