Rails: url vs path

I was wondering why some people are using _path and why some use _url. For example, if you have a Post resource you can write the following in your view:

<%= link_to 'List of posts', posts_path %>

or the following in the controller:

redirect_to posts_url

Why should I use _url in the controller. I have used _path and it seems to work.

This is an explanation I found in a forum and I quote:

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

Leave a Reply