Rails: What is the location option for in the render method

假如想象 提交于 2020-01-01 04:12:05

问题


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:

"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.


回答1:


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)

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 method does this by setting the Location option in response object

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

You can find details about Location header here http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30.




回答2:


The Location header is for redirecting the page.



来源:https://stackoverflow.com/questions/12084431/rails-what-is-the-location-option-for-in-the-render-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!