命名路由 _path 与 _url [英] Named routes _path vs _url

查看:35
本文介绍了命名路由 _path 与 _url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 提供命名路由.

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

Routes helper can be called using path or url

例如来自文档:

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

坦率地说,我从来没有使用过 *_url 助手,我能够使用 *_path 来让事情正常工作.

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

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

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

它们之间有什么不同?

一些真实的例子,解释什么时候使用什么是最好的.

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

推荐答案

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

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

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

_url helpers 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.)

这篇关于命名路由 _path 与 _url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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