What is the difference between _url and _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

Advertisements

One thought on “What is the difference between _url and _path

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s