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

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

问题描述

当我们在 routes.rb 中定义路由时使用类似 map.some_link 的名称.我们可以通过两种方式使用链接 - 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 用于视图,因为 ahrefs 隐式链接到当前 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天全站免登陆