在rails中使用路由时_url和_path有什么区别 [英] What is the difference between _url and _path while using the routes in rails

查看:94
本文介绍了在rails中使用路由时_url和_path有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用 map.some_link 之类的名称在 routes.rb 中定义路由时,可以使用链接有两种方式- some_link_url some_link_path

When we define routes in routes.rb using the name like map.some_link.We can use the link in two ways- some_link_url, some_link_path.


  • 两者之间有什么区别?

  • 使用哪个更安全?

推荐答案

我遇到了同样的问题,并且写了在我的博客中有关此内容的一则小帖子

I had the same question and I wrote a small post about this in my blog

原因总结如下(我在论坛上找到了这个原因):

The reason is summarized here (I found this on a forum):


* _ path用于视图,因为ahref是隐式链接到当前URL。因此,一遍又一遍地重复它会浪费字节。但是,在控制器中,redirect_to需要* _url,因为HTTP规范要求3xx重定向中的Location:标头是完整的URL。

*_path are for views because ahrefs are implicitly linked to the current URL. So it’d be a waste of bytes to repeat it over and over. In the controller, though, *_url is needed for redirect_to because the HTTP specification mandates that the Location: header in 3xx redirects is a complete URL.

这是另一种解释,其中说取决于从非SSL站点链接到SSL站点时是否需要使用绝对URI,反之亦然。

Here is another explanation which says it depends on whether we need to use an absolute URI when linking to an SSL site from a non-SSL site, and vice versa.

到目前为止,我所读到的内容,并不表明它们中的任何一个比另一个更安全。真正归结为什么是适当的用法。

What I have read so far, doesn't suggest that any of them is more secure than the other. It really comes down to what is the "proper" usage.

这篇关于在rails中使用路由时_url和_path有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆