Ruby-on-rails 命名路由 _path 与 _url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11939865/
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-03 03:51:37  来源:igfitidea点击:

Named routes _path vs _url

ruby-on-rails

提问by Pritesh Jain

Rails provides named routes.

Rails 提供命名路由

Routes helper can be called using path or url

可以使用路径或 url 调用路由助手

eg from docs:

例如来自文档:

# and provide these named routes
root_url   # => 'http://www.example.com/'
root_path  # => '/'

frankly speaking I have never used *_urlhelper yet , I was able to get things working using *_path.

坦率地说,我从未使用过*_urlhelper,我能够使用*_path.

I was bit confused what is the purpose of these two different helpers?

我有点困惑这两个不同的助手的目的是什么?

how are they different from one another?

它们之间有何不同?

some real examples with explanations when to use what would be great.

一些真实的例子,解释什么时候使用什么会很好。

回答by Chris Peters

_pathhelpers provide a site-root-relative path. You should probably use this most of the time.

_path帮助程序提供站点根目录相对路径。您可能应该在大部分时间使用它。

_urlhelpers provide an absolute path, including protocol and server name. I've found that I mainly use these in emails when creating links to the app on the server. They should mainly be used when providing links for external use. (Think email links, RSS, and things like the copy and paste URL field under a YouTube video's "Share" section.)

_urlhelpers 提供一个绝对路径,包括协议和服务器名称。我发现在创建指向服务器上的应用程序的链接时,我主要在电子邮件中使用这些。它们应该主要在提供供外部使用的链接时使用。(想想电子邮件链接、RSS 以及 YouTube 视频“共享”部分下的复制和粘贴 URL 字段等内容。)

回答by Mori

When you put a link in your own site, the domain part of the route is redundant, and adds to the page size, so you can just use the path part of the URL with the *_pathhelper. On the other hand, if the URL is to be consumed outside of your site, e.g. an email or an RSS feed, the whole URL is needed, so use the *_urlhelper.

当您在自己的站点中放置链接时,路由的域部分是多余的,并且会增加页面大小,因此您可以仅使用 URL 的路径部分与*_path帮助程序。另一方面,如果要在站点之外使用 URL,例如电子邮件或 RSS 提要,则需要整个 URL,因此请使用*_url帮助程序。

回答by Daniel Romero

As the other answers explain, you should use _urlin email links, etc. But I would like to add that you should also use _urlin redirects, as explained here:

正如其他答案所解释的,您应该_url在电子邮件链接等中使用。但我想补充一点,您也应该_url在重定向中使用,如下所述:

https://www.ruby-forum.com/topic/101346#221052

https://www.ruby-forum.com/topic/101346#221052

and, here:

和这里:

http://viget.com/extend/rails-named-routes-path-vs-url

http://viget.com/extend/rails-named-routes-path-vs-url

You can also take a look at the relevant section of the HTTP specification here:

您还可以在此处查看 HTTP 规范的相关部分:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

回答by S.D

_path provides relative path.

_path 提供相对路径。

_url provides absolute path.

_url 提供绝对路径。

Whenever you send a URL in email etc. than it is a best practice to use _url instead of _path.

每当您在电子邮件等中发送 URL 时,最佳做法是使用 _url 而不是 _path。