123

Rails provides named routes.

Routes helper can be called using path or url

eg from docs:

# and provide these named routes
root_url   # => 'http://www.example.com/'
root_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?

how are they different from one another?

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

Pritesh Jain
  • 9,106
  • 4
  • 37
  • 51

4 Answers4

174

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

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

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
14

When you put a link in your own site, the domain part of the route is redundant, and adds to the page size, so you can just use the path part of the URL with the *_path helper. On the other hand, if the URL is to be consumed outside of your site, e.g. an email or an RSS feed, the whole URL is needed, so use the *_url helper.

Mori
  • 27,279
  • 10
  • 68
  • 73
  • I've recently had a problem with links and domain, and the _url helper was my only means to get links using the proper domain and not "hosting-related" links. – Pierre-Adrien Aug 13 '12 at 18:41
  • 1
    I also use _url helper when have some subdomains in app. For example, when im on abc.example.com _path will redirect to abc.example.com/somesection when _url will redirect correctly to example.com/somesection – Wordica Dec 03 '13 at 20:48
12

As the other answers explain, you should use _url in email links, etc. But I would like to add that you should also use _url in redirects, as explained here:

https://www.ruby-forum.com/topic/101346#221052

and, here:

http://viget.com/extend/rails-named-routes-path-vs-url

You can also take a look at the relevant section of the HTTP specification here:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

Daniel Romero
  • 1,581
  • 1
  • 20
  • 33
  • 2
    Quoting the first link (in case it disappears): "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." – Terrabits Apr 07 '16 at 19:03
4

_path provides relative path.

_url provides absolute path.

Whenever you send a URL in email etc. than it is a best practice to use _url instead of _path.

S.D
  • 485
  • 5
  • 8
  • 3
    Downvoting because this is exactly what Chris Peters had already answered over two years prior to this answer. Redundant answers are not helpful. – jeffdill2 Sep 11 '18 at 14:15
  • 1
    upvoting because it's less visually noisy, you get your answer without having to parse opinion phrases like "you should probably" or "i've found that" or "they should mainly be used" – worc Dec 13 '21 at 22:03