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
.
当我们定义路线的时候。使用名称如map.some_link。我们可以用两种方式使用这个链接——some_link_url, some_link_path。
- What are the differences between the two?
- 这两者之间有什么区别?
- Which is more secure to be used?
- 哪一种更安全?
7 个解决方案
#1
72
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 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.
*_path用于视图,因为ahrefs隐式地链接到当前URL。所以一遍又一遍地重复是浪费字节。但是在控制器中,redirect_to需要*_url,因为HTTP规范要求3xx重定向中的Location: header是一个完整的URL。
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.
这里还有另一种解释,说明它取决于从非SSL站点链接到SSL站点时是否需要使用绝对URI,反之亦然。
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.
到目前为止我所读到的,并不意味着其中任何一个都比另一个更安全。真正的问题是什么是“合适”的用法。
#2
70
path
is relative while url
is absolute.
路径是相对的,url是绝对的。
#3
33
An example of the difference for a resource called "user":
一个名为“user”的资源的差异示例:
users_url # => http://localhost:3000/users
users_path # => /users
#4
2
Same answer as Petros, except that modern browsers handle relative redirects just fine. (I'd comment on his answer, but I can't yet.)
和Petros的答案一样,只是现代浏览器可以处理相对重定向。(我想评论一下他的回答,但我还不能。)
#5
1
By secure if you mean not exposing all the data passed, then _path is better as it generates a relative url, something like '/login' but _path would give 'http://localhost:3000/login'. Please refer to this blog post i found sometime back regarding the same. When _url is better than _path
如果您的意思是不公开所传递的所有数据,那么_path会更好,因为它生成一个相对url,类似于'/login',但是_path会生成'http://localhost:3000/login'。请参考我以前找到的这篇博文。当_url优于_path时
#6
1
_url will give the entire path. As it contains the domain name and protocol, you can use it for eg. to send email or redirecting to another domain, etc.
_url将给出整个路径。由于它包含域名和协议,您可以将它用于eg。发送电子邮件或重定向到另一个域等。
_path will return the path which is after '/' without domain,protocol etc. So you can use it every now and then(I guess), where you don't require details of domain.
_path会返回在'/'之后的路径,没有域、协议等,所以你可以时不时地使用它(我猜),在那里你不需要域的细节。
#7
0
The _url
helper generates a string containing the entire URL, while the _path
helper generates a string containing the relative path from the root of the application, e.g.:
_url helper生成一个包含整个URL的字符串,而_path helper生成一个包含来自应用程序根的相对路径的字符串,例如:
photos_url # => "http://www.example.com/photos"
photos_path # => "/photos"
As per Rails Guides - Routing.
根据Rails指南-路由。
#1
72
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 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.
*_path用于视图,因为ahrefs隐式地链接到当前URL。所以一遍又一遍地重复是浪费字节。但是在控制器中,redirect_to需要*_url,因为HTTP规范要求3xx重定向中的Location: header是一个完整的URL。
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.
这里还有另一种解释,说明它取决于从非SSL站点链接到SSL站点时是否需要使用绝对URI,反之亦然。
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.
到目前为止我所读到的,并不意味着其中任何一个都比另一个更安全。真正的问题是什么是“合适”的用法。
#2
70
path
is relative while url
is absolute.
路径是相对的,url是绝对的。
#3
33
An example of the difference for a resource called "user":
一个名为“user”的资源的差异示例:
users_url # => http://localhost:3000/users
users_path # => /users
#4
2
Same answer as Petros, except that modern browsers handle relative redirects just fine. (I'd comment on his answer, but I can't yet.)
和Petros的答案一样,只是现代浏览器可以处理相对重定向。(我想评论一下他的回答,但我还不能。)
#5
1
By secure if you mean not exposing all the data passed, then _path is better as it generates a relative url, something like '/login' but _path would give 'http://localhost:3000/login'. Please refer to this blog post i found sometime back regarding the same. When _url is better than _path
如果您的意思是不公开所传递的所有数据,那么_path会更好,因为它生成一个相对url,类似于'/login',但是_path会生成'http://localhost:3000/login'。请参考我以前找到的这篇博文。当_url优于_path时
#6
1
_url will give the entire path. As it contains the domain name and protocol, you can use it for eg. to send email or redirecting to another domain, etc.
_url将给出整个路径。由于它包含域名和协议,您可以将它用于eg。发送电子邮件或重定向到另一个域等。
_path will return the path which is after '/' without domain,protocol etc. So you can use it every now and then(I guess), where you don't require details of domain.
_path会返回在'/'之后的路径,没有域、协议等,所以你可以时不时地使用它(我猜),在那里你不需要域的细节。
#7
0
The _url
helper generates a string containing the entire URL, while the _path
helper generates a string containing the relative path from the root of the application, e.g.:
_url helper生成一个包含整个URL的字符串,而_path helper生成一个包含来自应用程序根的相对路径的字符串,例如:
photos_url # => "http://www.example.com/photos"
photos_path # => "/photos"
As per Rails Guides - Routing.
根据Rails指南-路由。