Ruby-on-rails 使用rails中的路由时_url和_path有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2350539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:20:18  来源:igfitidea点击:

What is the difference between _url and _path while using the routes in rails

ruby-on-railsroutes

提问by MohamedSanaulla

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

当我们routes.rb使用类似名称定义路由时map.some_link。我们可以通过两种方式使用链接 - some_link_urlsome_link_path

  • What are the differences between the two?
  • Which is more secure to be used?
  • 两者之间有什么区别?
  • 使用哪个更安全?

回答by Petros

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: 标头是一个完整的 URL。

Here is another explanationwhich 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.

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

回答by ponzao

pathis relative while urlis absolute.

path是相对的,而url绝对的。

回答by Jason

An example of the difference for a resource called "user":

名为“user”的资源的差异示例:

users_url # => http://localhost:3000/users
users_path  # => /users

回答by Ian Lotinsky

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 的答案相同,只是现代浏览器处理相对重定向就好了。(我会对他的回答发表评论,但我还不能。)

回答by Radhika

_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 将给出整个路径。由于它包含域名和协议,因此您可以将其用于例如。发送电子邮件或重定向到另一个域等。

_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 将返回没有域、协议等的 '/' 之后的路径。所以你可以时不时地使用它(我猜),你不需要域的详细信息。

回答by Alok Swain

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

如果您的意思是不公开所有传递的数据,那么安全性会更好,因为它会生成一个相对 url,例如“/login”,但 _path 会给出“ http://localhost:3000/login”。请参考我在某个时候发现的这篇博文。当 _url 优于 _path

回答by jbk

The _urlhelper generates a string containing the entire URL, while the _pathhelper generates a string containing the relative path from the root of the application, e.g.:

_url辅助生成包含整个URL的字符串,而_path辅助生成包含从应用程序,例如,根目录的相对路径的字符串:

photos_url  # => "http://www.example.com/photos"
photos_path # => "/photos"

As per Rails Guides - Routing.

根据Rails 指南 - 路由