Rails中命名路由的区别***_url与***_path

Rails中命名路由的区别***_url与***_path

 

意外

为了安全,端口号跳来跳去,从3000跳到4000再跳到5000。。。8000,最后rails和nginx都没有得到最正确的端口号。

这不要紧,最要紧的是命名路由出问题了,很多***_url的写法都得不到正确的端口号,跳到80了。

 

命名路由

path是相对路径
url是绝对路径
model为复数的会对应到action=>index, 为单数时需要传递id参数并对应到action=>show

如对于user而言:
users_url # => http://localhost:3000/users
users_path # => /users
user_path(1) # => /users/1
user_url(1) # => http://localhost:3000/users/1

 

区别***_url与***_path

参考这里:Rails Named Routes: Path Vs. URL

So when do you use paths, and when do you use URLs? I had heard that there are two cases when you must use URLs:

  1. You need to use an absolute URI when linking to an SSL site from a non-SSL site, and vice versa.
  2. You need to use an absolute URI when creating a redirect (e.g. with redirect_to.)

作者也提出了这个问题,也许吧,他也碰到这个情况。

Probably not; most modern HTTP clients and servers seem to figure out what you meant if you pass a relative URL in a redirect. But why take the chance when it’s easy to do the right thing?

 

小结:

特殊需求害死程序员。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值