What is the difference between _url and _path while using the routes in rails

前端 未结 7 2044
萌比男神i
萌比男神i 2020-12-04 07:27

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_p

相关标签:
7条回答
  • 2020-12-04 07:48

    Same answer as Petros, except that modern browsers handle relative redirects just fine. (I'd comment on his answer, but I can't yet.)

    0 讨论(0)
  • 2020-12-04 07:53

    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 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.

    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.

    0 讨论(0)
  • 2020-12-04 08:02

    An example of the difference for a resource called "user":

    users_url # => http://localhost:3000/users
    users_path  # => /users
    
    0 讨论(0)
  • 2020-12-04 08:04

    _url will give the entire path. As it contains the domain name and protocol, you can use it for eg. to send email or redirecting to another domain, etc.

    _path will return the path which is after '/' without domain,protocol etc. So you can use it every now and then(I guess), where you don't require details of domain.

    0 讨论(0)
  • 2020-12-04 08:09

    By secure if you mean not exposing all the data passed, then _path is better as it generates a relative url, something like '/login' but _path would give 'http://localhost:3000/login'. Please refer to this blog post i found sometime back regarding the same. When _url is better than _path

    0 讨论(0)
  • 2020-12-04 08:10

    path is relative while url is absolute.

    0 讨论(0)
提交回复
热议问题