Ruby Forum Ruby on Rails > path vs. url

Posted by stevemolitor@gmail.com (Guest)
on 15.03.2007 00:41
(Received via mailing list)
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
Posted by Josh Susser (jsusser)
on 15.03.2007 01:09
stevemolitor@gmail.com 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
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 Susser
http://blog.hasmanythrough.com
Posted by DHH (Guest)
on 15.03.2007 01:25
(Received via mailing list)
> 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).
Posted by john (Guest)
on 15.03.2007 10:06
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"... :) )
Posted by Russell Norris (Guest)
on 15.03.2007 16:22
(Received via mailing list)
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
Posted by David A. Black (Guest)
on 15.03.2007 18:06
(Received via mailing list)
Hi --

On 3/14/07, Josh Susser <rails-mailing-list@andreas-s.net> 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)
   (See what readers are saying!  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)
Posted by DHH (Guest)
on 15.03.2007 18:07
(Received via mailing list)
> 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.
Posted by David A. Black (Guest)
on 15.03.2007 19:04
(Received via mailing list)
Hi --

On 3/15/07, DHH <david.heinemeier@gmail.com> 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)
   (See what readers are saying!  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)
Posted by Russell Norris (Guest)
on 15.03.2007 23:58
(Received via mailing list)
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
Posted by Adrian De la cruz (silverfang)
on 02.08.2009 01:11
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:
http://stackoverflow.com/questions/1150339/rails-2-3-2-trying-to-render-erb-instead-of-haml
Posted by Fernando Perez (fernando)
on 15.12.2009 02:01
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 - VOD et visioconference