Nest Those Rails Resources Or Make Baby Semantic Web Cry

Proper web architecture dictates that a you should "Assign distinct URIs to distinct resources." And Cool URIs for the Semantic Web states that:


There should be no confusion between identifiers for Web documents and identifiers for other resources. URIs are meant to identify only one of them, so one URI can't stand for both a Web document and a real-world object.


So we know that a URI should refer to one and only one resource. (Of course, you may have many URIs all referring to the same resource.) So why do so many web sites have URIs like http://www.example.org/myaccount? That same URI is used to refer to any account in the system, depending on who is logged in. And that makes Baby Semantic Web cry.

Why is the baby sobbing? A generic URI like http://www.example.com/myaccount isn't useful on the semantic web, because it's very difficult to make meaningful statements about that URI. Let's go and try.


http://www.example.com/myaccount is the account page of "Seth Ladd".


and


http://www.example.com/myaccount is the account page of "Bob Smith".


Hmm... so http://www.example.com/myaccount is the account page for both Seth and Bob? That doesn't make much sense!

A better URI for an account page would be http://www.example.com/accounts/23232, which is easily unique for every user.

The moral of this story is that every one of your URIs should be unique. So let's bring this all the way back to Rails and resources.

When building your resources, ask yourself, "If I GET this URI, will I see the same thing no matter who is logged in?" If the answer is "No" then you need to nest your resources so that the URI is unique and the same representation is returned no matter who you are logged in as.

For example, a typical URI would be http://www.example.com/books, which could easily be a collection of books for the user. The contents of that URI are relative to the person logged in, so we have a problem. To fully qualify the URI, we need to nest books inside of the user collection. We end up with http://www.example.com/users/1/books, which is unique and follows web architecture best practices. Now we can say unambiguous statements about the URI, thus populating the semantic web with more useful and meaningful triples.

Popular posts from this blog

The 29 Healthiest Foods on the Planet

Lists and arrays in Dart

Converting Array to List in Scala