# Path vs. url

**URL:** <https://www.ruby-forum.com/t/path-vs-url/88852>\
**Category:** Rails\
**Created:** [March 15, 2007, 12:41am UTC](https://www.ruby-forum.com/t/path-vs-url/88852 "2007-03-15T00:41:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 12:41am UTC](https://www.ruby-forum.com/t/path-vs-url/88852/1 "2007-03-15T00:41:42Z")

</div>

When using named RESTful routes, when should one use the  
generated …\_path helpers, and when the …\_url helpers? Say I did  
this in routes.rb:

map.resources :users

Where should I use users\_path, new\_user\_path, etc., and where should I  
use users\_url, new\_user\_url…?

Thanks!

Steve

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 1:09am UTC](https://www.ruby-forum.com/t/path-vs-url/88852/2 "2007-03-15T01:09:46Z")

</div>

removed\_email\_address@domain.invalid wrote:

> When using named RESTful routes, when should one use the  
> generated …\_path helpers, and when the …\_url helpers? Say I did  
> this in routes.rb:
> 
> map.resources :users
> 
> Where should I use users\_path, new\_user\_path, etc., and where should I  
> use users\_url, new\_user\_url…?
> 
> Thanks!
> 
> Steve

The users\_url helper generates a URL that includes the protocol and host  
name. The users\_path helper generates only the path portion.

users\_url: [http://localhost/users](http://localhost/users)  
users\_path: /users

Mostly you should use the \_path flavor. If you need to spec the host or  
protocol (like for talking to another app or service), then use the \_url  
flavor.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 1:25am UTC](https://www.ruby-forum.com/t/path-vs-url/88852/3 "2007-03-15T01:25:44Z")

</div>

> Mostly you should use the \_path flavor. If you need to spec the host or  
> protocol (like for talking to another app or service), then use the \_url  
> flavor.

Usually the rule is \_path in views, \_url in controller (where you  
mostly use it together with redirect\_to).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 10:06am UTC](https://www.ruby-forum.com/t/path-vs-url/88852/4 "2007-03-15T10:06:54Z")

</div>

DHH wrote:

> > Mostly you should use the \_path flavor. If you need to spec the host or  
> > protocol (like for talking to another app or service), then use the \_url  
> > flavor.
> 
> Usually the rule is \_path in views, \_url in controller (where you  
> mostly use it together with redirect\_to).

and what about to use everytime url?  
(i think that the DHH’s reply will be: “you’re insaaaane”… 🙂 )

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/path-vs-url/88852/5 "2007-03-15T16:22:41Z")

</div>

David [or anyone], would you mind explaining why you wouldn’t want to  
use  
the \_path form in a controller. I’ve never done it but I don’t  
understand  
why it’s bad form. Thanks.

RSL

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/path-vs-url/88852/6 "2007-03-15T18:06:58Z")

</div>

Hi –

On 3/14/07, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Mostly you should use the \_path flavor. If you need to spec the host or  
> protocol (like for talking to another app or service), then use the \_url  
> flavor.

I guess a case could be made, though, that for REST compliance, you’d  
want to use \_url throughout, as \_path doesn’t give you a unique  
identifier that can be used to locate a resource outside of this  
particular browser session.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/path-vs-url/88852/7 "2007-03-15T18:07:58Z")

</div>

> I guess a case could be made, though, that for REST compliance, you’d  
> want to use \_url throughout, as \_path doesn’t give you a unique  
> identifier that can be used to locate a resource outside of this  
> particular browser session.

I’m not sure I follow? \*\_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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/path-vs-url/88852/8 "2007-03-15T23:58:55Z")

</div>

Thanks, David. That makes complete sense. I got why you’d want to use  
\_path  
in the views. I just didn’t know that bit about the HTTP specs.

RSL

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/path-vs-url/88852/9 "2007-03-15T19:04:06Z")

</div>

Hi –

On 3/15/07, DHH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I guess a case could be made, though, that for REST compliance, you’d  
> > want to use \_url throughout, as \_path doesn’t give you a unique  
> > identifier that can be used to locate a resource outside of this  
> > particular browser session.
> 
> I’m not sure I follow? \*\_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.

I’m overstating it (didn’t mean to dismiss all relative URLs 🙂 –  
just thinking out loud about a case where the hyperlinks in a response  
might be needed to identify resources but the response not contain the  
necessary base URL identification. I’m not coming up with any great  
sample scenarios, though.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 2, 2009, 1:11am UTC](https://www.ruby-forum.com/t/path-vs-url/88852/10 "2009-08-02T01:11:45Z")

</div>

DHH wrote:

> > Mostly you should use the \_path flavor. If you need to spec the host or  
> > protocol (like for talking to another app or service), then use the \_url  
> > flavor.
> 
> Usually the rule is \_path in views, \_url in controller (where you  
> mostly use it together with redirect\_to).

I’m getting an error when trying to use a resource\_url helper inside my  
controller to render a view.

Here is the problem explained:

> <https://stackoverflow.com/questions/1150339/rails-2-3-2-trying-to-render-erb-instead-of-haml>

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 15, 2009, 2:01am UTC](https://www.ruby-forum.com/t/path-vs-url/88852/11 "2009-12-15T02:01:42Z")

</div>

DHH wrote:

> > I guess a case could be made, though, that for REST compliance, you’d  
> > want to use \_url throughout, as \_path doesn’t give you a unique  
> > identifier that can be used to locate a resource outside of this  
> > particular browser session.
> 
> I’m not sure I follow? \*\_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.  
> This thread is old but still shows up in top rank in search engines.

Here is my advise: use \_url all the time. Yeah it wastes a few bytes,  
but the day you’ll add https support to your site it will save you tons  
of headaches. Same applies to images that will end up in RSS feeds.  
Using \_path would give them a feed:// protocol.

So save yourself troubles and use \_url all the time, period.

Regards,

–  
[http://www.digiprof.tv](http://www.digiprof.tv) - VOD et visioconference

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 20, 2010, 11:19pm UTC](https://www.ruby-forum.com/t/path-vs-url/88852/12 "2010-12-20T23:19:12Z")

</div>

> Here is my advise: use \_url all the time. Yeah it wastes a few bytes,  
> but the day you’ll add https support to your site it will save you tons  
> of headaches.

How would it save headaches in case of https?.

Lets say you have apache in front of your rails server and only apache  
is configured for https. And http is disabled in apache.

Now with \_url option, the web page gets generated as

\<form ... action="http://host/resource/1" ...

And apache passes it along as it as, unless one enables URL rewriting in  
apache to replace http with https.

Where as if it is just \_path it gets generated as

\<form ...action="/resource/1" ...

This form submission works irrespective of how(http or https) apache is  
configured.

So the earlier recommendation ( i.e., use \_path in views \_url in  
specific use cases) is still a better one compared to use \_url in all  
cases. (no period 😉 )

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 6, 2011, 11:51pm UTC](https://www.ruby-forum.com/t/path-vs-url/88852/13 "2011-03-06T23:51:26Z")

</div>

Here’s a reason where I wish I was using \_url everywhere.

I’ve switched my site to almost all ssl except for a google map page  
which needs to be in non-ssl ($10,000 for the ssl version). On that  
page (just like every other page) are about 15-20 navigation links that  
should point to https/ssl pages even though the map page itself is http.

I wish I wouldn’t have to go through and change all those links to point  
to https using \_url. I could instead use ssl\_requirement and specify  
all pages should be ssl except for my map page, but I think I’d rather  
make all 15-20 links of navigation use the \_url instead and force https  
to avoid an unneeded redirect/latency. I actually have redirects in  
apache conf file instead of using ssl\_requirement, but same idea…why  
redirect when you can specify \_url and have less latency?

If you have a solution for me that would relieve my need to have \_url  
for all my navigation due to the one http map page, please, let me know.

Kevin

Madhav V. wrote in post #969656:

> How would it save headaches in case of https?.
