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:
- You need to use an absolute URI when linking to an SSL site from a non-SSL site, and vice versa.
- 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?
小结:
特殊需求害死程序员。