Difference Between Path and URL in Rails

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

Post last updated on May 13, 2023