# RESTful interfaces... question

**URL:** <https://www.ruby-forum.com/t/restful-interfaces-question/115553>\
**Category:** Rails\
**Created:** [September 26, 2007, 6:52am UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553 "2007-09-26T06:52:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 26, 2007, 6:52am UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/1 "2007-09-26T06:52:45Z")

</div>

\*\*\* I’m going to preface this comment/question with a disclaimer: I’m  
really annoyed at this particular second with the buzzwordy-nature of  
“REST”, at least in the context of the information I’ve found about it  
online. It seems to be an abstract concept that people only pretend  
they understand, but they really don’t, so they throw around all of the  
same jargon–on pretty much EVERY site about it–as everyone else, when  
in reality they don’t really know why they like it or exactly what its  
requirements are. Maybe I’m wrong, but that’s my impression and that’s  
why I’m annoyed. \*\*\*

Okay. In order to simplify my post, let’s just pretend I’m developing a  
lightweight social networking site, and I have Users and UserProfiles  
(with the obvious has\_one association). Is it considered “RESTful” if I  
have these following routes? Is the “RESTful” requirement simply that  
there is some cacheable URI for each thing for each of the CRUD  
operations?

Home is only seen by the logged in user, and might have all sorts of  
“resource” information on it…

/user/home

These guys represent 2 different “resources”, UserProfile and Comment  
/username/profile  
/username/profile;edit  
/username/comments  
/username/comments?comment\_id=1234

What if each user has a forum, but there is also a public forum?

/username/forum/thread?thread\_id=1234  
/forums/thread?forum\_id=1234&thread\_id=1234

Would those 2 routes constitute a “RESTful” interface?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 26, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/2 "2007-09-26T15:27:28Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 26, 2007, 7:27pm UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/3 "2007-09-26T19:27:44Z")

</div>

Hi,

On 9/26/07, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> why I’m annoyed. \*\*\*
> 
> Okay. In order to simplify my post, let’s just pretend I’m developing a  
> lightweight social networking site, and I have Users and UserProfiles  
> (with the obvious has\_one association). Is it considered “RESTful” if I  
> have these following routes? Is the “RESTful” requirement simply that  
> there is some cacheable URI for each thing for each of the CRUD  
> operations?

I would really recommend checking out RESTful Web Services by Leonard  
Richardson and Sam Ruby. It clarifies the subject material quite well.

> **[RESTful Web Services](https://www.oreilly.com/library/view/restful-web-services/9780596529260/)**
>
> "Every developer working with the Web needs to read this book."-- David Heinemeier Hansson, creator of the Rails framework "RESTful Web Services finally provides a practical roadmap for constructing services … - Selection from RESTful Web Services...

Home is only seen by the logged in user, and might have all sorts of

> What if each user has a forum, but there is also a public forum?
> 
> /username/forum/thread?thread\_id=1234  
> /forums/thread?forum\_id=1234&thread\_id=1234
> 
> ## Would those 2 routes constitute a “RESTful” interface?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Michael G.

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 27, 2007, 5:33am UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/4 "2007-09-27T05:33:01Z")

</div>

Thanks for the replies guys.

I’m still a bit confused about where to draw the line between different  
“resources”. Like a User and a UserProfile. What if I want  
/someusername to effectively act like /userprofile/##? Is that still  
RESTful? Or to comply with the idea of RESTful interfaces, do I have to  
stick with those limiting URLs you always see… /user/##, /profile/##,  
etc… ??

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 27, 2007, 6:29am UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/5 "2007-09-27T06:29:04Z")

</div>

As far as I’m concerned draw the line anywhere you see fit. If you  
want a User that has one or many UserProfiles then have at it. In that  
case you may want to consider making UserProfile a nested resource of  
User, if the case is that UserProfile doesn’t make sense on it’s own  
without being scoped by User. Then you would have a URL like /users/1/  
user\_profiles/1 if your getting a single resource or /users/1/  
user\_profiles if your getting the collection. Assuming you’re using  
the GET method. If you use POST to /users/1/user\_profiles then you  
would be create a new UserProfile under the User. Of course you’ll  
also need to make sure you controller actions are setup properly to  
assign the new UserProfile to the User.

I don’t really see what you feel is so limiting about the RESTful URL.  
They are far “prettier” than the traditional ones. Besides that there  
is nothing stopping you from using standard or named routes wherever  
they make sense. REST adds to Rails, it doesn’t really limit it in  
any way that I can see.

On Sep 26, 11:33 pm, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 26, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/6 "2007-09-26T19:53:00Z")

</div>

In order to really understand the nature of REST you need to put it up  
against the alternative.

Here is a link to what convinced me:  
[http://www.wanderingbarque.com/nonintersecting/2006/11/15/the-s-stands-for-simple/](http://www.wanderingbarque.com/nonintersecting/2006/11/15/the-s-stands-for-simple/)

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 27, 2007, 6:30am UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/7 "2007-09-27T06:30:50Z")

</div>

You could nest them and do something like users/1/profile

–Jeremy

On 9/26/07, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [September 27, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/restful-interfaces-question/115553/8 "2007-09-27T13:19:44Z")

</div>

On 9/26/07, Dave S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
\<Dave’s Rant Snipped\>

REST-as-religion gets annoying alright, but be clear - the religious  
aspect is not coming from the chief advocates, but the recently  
converted.

The main problem you are arriving at, is that much of the stuff on  
REST is highly academic, and REST web services are not widely  
supported in many web frameworks (only the good ones). In  
particular the Java guys haven’t generally drunk the Kool-aid, so  
that massive pool of developers have so far been absent from the  
discussion.

> Okay. In order to simplify my post, let’s just pretend I’m developing a  
> lightweight social networking site, and I have Users and UserProfiles  
> (with the obvious has\_one association). Is it considered “RESTful” if I  
> have these following routes? Is the “RESTful” requirement simply that  
> there is some cacheable URI for each thing for each of the CRUD  
> operations?

I am not a RESTafarain (yet), but I think I grok it. Here’s my take,  
hopefully  
others can address anything I get wrong:

The primary element in REST is the resource. Resources should be  
mappable to a URI, and interacted with exclusively by HTTP verbs.  
The URI is exclusively they way that you locate a resource. The  
same URI cannot be mapped to different resources and still be  
RESTful. (say if you were using cookies or sessions or something)

- So a RESTful route should not use the URL query string (anything after  
the ?) to identify the resource. (this isn’t a hard and fast rule, you  
can  
use the query string and still be RESTful though its harder work)

- A RESTful route should _not_ contain a verb. This is an absolute.

> Home is only seen by the logged in user, and might have all sorts of  
> “resource” information on it…
> 
> /user/home

Not RESTful. /user/home should always return the _same_ resource in  
order to be restful. To achieve what you want to achieve, you would use  
the following:

/user/1/home  
/user/dsmith/home  
/user/rconroy/home  
/user/spiderman\_045/home

or any variation of /user/${UNIQUE\_USER\_REFERENCE}/home

> These guys represent 2 different “resources”, UserProfile and Comment  
> /username/profile

From above, if you do the following:

/davesmith/profile  
and  
/richard/profile

these are RESTful.

> /username/profile;edit

Not RESTful. Verb in URI.

> /username/comments?comment\_id=1234

If you were being a REST fundamentalist, this isn’t RESTful, but more  
practical RESTafarians recognise this as a necessary workaround in  
some cases. In  
a practical sense, you would probably use a simpler route format too:

/username/comments/1234

> What if each user has a forum, but there is also a public forum?
> 
> /username/forum/thread?thread\_id=1234  
> /forums/thread?forum\_id=1234&thread\_id=1234
> 
> Would those 2 routes constitute a “RESTful” interface?

There is a couple of misconceptions about REST.

- Every resource in your site has to be RESTful

People really struggle with this. And with good reason: it is  
impractical to implement everything as purely RESTful, when  
you expect to serve pages to browsers. You can’t realistically  
have a form on every page, and browsers don’t even support all  
the HTTP verbs anyway. So you end up using a lot of workarounds  
(AJAX, non-RESTful form resources that POST/PUT/DELETE  
RESTful resources etc.).

So you end up with  
/myforum/thread/12/post/12345 (RESTful)

to edit that post then:  
/myforum/thread/12/post/12345/edit (not-RESTful)  
However it is essential that the form actions point towards a genuine  
RESTful resource.

- People have trouble modelling their domain object hierarchies in  
a RESTful way

Well thats the thing - REST has no concept of this. As users of a  
RESTful web resource you expect that there is some logical hierarchy  
or naming convention of RESTful routes. But its just a convention  
imposed by the designer of that RESTful site.

REST only requires that your current resource denotes other related  
resources as URLs. Those other URLs do not have to be specialised  
forms of the current resource.

Consider a social networking site where I can list my friends:  
/users/rconroy/friends  
This returns a list of other users. However the links to those  
users have the form:  
/users/dsmith/profile  
/users/happygurl\_12/profile  
/users/axelrod/profile

These are my findings to date. I hope this helps your understanding.  
I would also be interested in seeing what people who _really_ know  
what they are talking about have to say about what I have said.
