Close and Go BackBack to Viget

Rails Named Routes: Path Vs. URL

Mark Cornick
Mark Cornick, Former Staffer, February 20, 2009 6

As we all learned in Rails 101, named routes come in two varieties: path, and URL. When should we use each variety?

Let's say we have this entry in a routes.rb file:

map.resources :doohickeys

So, we can refer to doohickeys_path, which returns a relative URI:

link_to 'index of doohickeys', doohickeys_path # => <a href="/doohickeys">index of doohickeys</a>

And we can refer to doohickeys_url, which returns an absolute URI:

link_to 'index of doohickeys', doohickeys_url  # => <a href="http://www.example.com/doohickeys">index of doohickeys</a>

So when do you use paths, and when do you use URLs? I had heard that there are two cases when you must use URLs:

  1. You need to use an absolute URI when linking to an SSL site from a non-SSL site, and vice versa.
  2. You need to use an absolute URI when creating a redirect (e.g. with redirect_to.)

In talking with some fellow developers here at Viget, and at Thoughtbot when I trained there recently, we agreed that the Rails convention seems to be to use URLs in these two cases, and paths in other cases. But why?

The first case is pretty obvious; you need the absolute URI in order to get the https:// prefix for a secure site (and likewise to get the http:// prefix if you need to get back to where you were.) For the second case, you need to dig into the HTTP specification, also known as RFC 2616. In particular, you need to find section 14.30, which defines the Location header that’s returned in a redirect. Here’s the chapter and verse:

The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. The field value consists of a single absolute URI.

(Emphasis added.) So that’s the answer: we do it this way because the specification says we have to. Since we’re all about conventions in the Rails community, we may as well have our convention follow the spec. Is it critical to do so? Probably not; most modern HTTP clients and servers seem to figure out what you meant if you pass a relative URL in a redirect. But why take the chance when it’s easy to do the right thing?

Jim Neath said on 02/20 at 03:24 PM

You should use root_url instead of root_path when linking to your homepage. There’s an SEO reason for this, but alas I can’t remember what it is.

When it comes to redirect_to I always use the _path variant. I think this may be a bad habit I’ve picked up somewhere. I’ll have to make sure I use the _url method instead, in future. Thanks for the tip.

Mark Cornick said on 02/20 at 03:54 PM

Thanks, Jim. I’ll ask one of our SEO experts to speak to the root_url suggestion; it sounds reasonable to me.

Anton Jenkins said on 02/20 at 04:06 PM

It’s good to have some guidance on this because I can’t remember reading anywhere else about the pros and cons of either. When you find a solution that works you stop looking. I’ve almost exclusively been using relative paths because they’ve worked, but I’ll certainly pay attention to when I should be using absolute paths from now on.

Oh, and I love that bit of javascript you’ve got which expands the code sample boxes to reveal all the code! I’m ashamed to say I’ve been flicking my mouse over them for at least 10 seconds! :oD

Tammer Saleh said on 02/20 at 04:50 PM

Is there any good reason to use the *_path helpers?  I often find myself annoyed that rails gives us both.  The content is generated, so I see no reason to use relative url paths.

Mark Cornick said on 02/20 at 04:59 PM

That’s a good question, Tammer. I suppose there are a few cases when relative URLs might be useful. Perhaps an application intentionally responds at more than one URL, or responds to both http:// and https:// URLs, not switching SSL on and off in the process?

If neither of those applies (and, honestly, the first one seems like a rare case at best, and a cookie-handling nightmare at worst) then choosing to use only absolute URLs sounds reasonable. After all, we’ve identified the cases where they MUST be used, but we have no analogous cases for relative URLs.

Josh Chambers said on 02/20 at 06:15 PM

@Jim Neath: You are correct. From an SEO perspective, you always want to use absolute URL paths. From what I know, it doesn’t make a difference to spiders. However, where it does make a difference if your content is scraped or re-published. In the event that your content is re-published/scraped, it can actually help your SEO by having links back to your site vs. broken paths. That’s the only SEO reason I know of.

Commenting is not available in this weblog entry.

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

Your post has made me think!

We people get used to that what we daily do. And normally we forget that we have to evolve.

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


How many days in a non-leap year?

Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.