Difference Between Path and URL in Rails
In Ruby on Rails Dec 6, 2021
Updated on May 13, 2023
In Rails, we often use the _path and _url helpers to link to various resources in our applications. But, what’s the difference between the two?
Both return URI references to web resources, but they do it differently.
What’s a URI? A URI is a way to identify a resource. It has a generic syntax, which defines a hierarchical sequence of five components: scheme, authority, path, query, and fragment. Not all URIs have all these five components. Only the scheme and the path components are required.
A URI begins with a scheme name followed by a colon separator. A scheme name refers to a specification for assigning
identifiers within that scheme. In the case of a web application, it is the
https
or the http
scheme name we use.
The authority component is used to refer to an authority using a domain name or web address. It also can include a username and a port. The authority component is preceded by a double slash.
The path, query, and fragment are used to identify a resource. If an authority component is present, the path must be empty or start with a slash.
The Rails _url
helper returns such a URI, more precisely a special type of it, that is, a Uniform
Resource Locator (URL). A URL not only identifies a resource but also provides a mechanism for accessing it.
The Rails _url
helper returns a URL that contains a scheme, an authority, and a path.
But we can make references to web resources without using the full syntax of a URI. These references that do not start with a scheme followed by a colon separator, are called relative references.
A widely used relative reference is a reference that begins with a slash and contains only the path to the resource,
without any reference to protocol and host. A reference like this is called an absolute-path reference. The Rails
_path
helper returns such an absolute-path reference.
Bibliography
- 1. Berners-Lee, T., Fielding, R., and L. Masinter, “Uniform Resource Identifier (URI): Generic Syntax”, STD 66, RFC 3986, January 2005.
- 2. “Rails Routing from the Outside In”, Rails Guides, https://guides.rubyonrails.org/routing.html