One of the biggest changes in Rails 2.0 is the way the framework pimps RESTful design.

??!@#$%^&*??

My thoughts exactly. Understanding what REST was/did/accomplsiehd was a bit of a struggle for me, hopefully, I can do a good job of breaking the concept down into tasty, digestible portions.

Disclaimer:
If you’re a robotic code monkey like myself, my over-simplified terminology may anger you. By all means, if I am incorrect, which I often am…correct me…but please…think of the kids! Don’t nitpick, it’s just not cool.

The HTTP Request

Before I can explain what REST is, you need to have a basic understand of how your web browser communicates with web servers. Let’s start with something you are familiar with. When you visited my blog, this is what happened:

  1. You typed in www.jonathansng.com

  2. Your browser sent a request to the server

  3. My server responded with HTML

  4. Your browser rendered that HTML

Well…beneath all this seemingly simple transaction, a few more, albeit important things are occurring. Using the same example as above, this is what is really happening:

  1. You typed in www.jonathansng.com

  2. Your browser sent a request to the server (called an HTTP request)

  3. My server responded to that HTTP request

  4. Your browser rendered the HTML that was returned

A HTTP request is like a little sticky note that is passed on to the web server. When the web server gets this note, it can read over the note and determine what action it needs to take place. There are eight different kinds of HTTP requests.

There are two kinds of requests that you should be most comfortable with. The “GET” request and the “POST” request. The “GET” request does exactly what it states, it gets the content of some resource from the server, identified by the URL Working with HTML, it should come as no surprise that the “POST” request is used to send (or POST) data to a URL, eventually creating a new resource.

Resources

I hate to use technical jargon, but some things can’t be side-stepped. Okay, take a deep breath and clear your mind. Stop thinking of the internet as “web pages”, from now on when you’re in Rails land, you need to think of the internet as a “resource.”

You might be a little bit confused, because as I’m writing this, I’m trying to think of a good example. Okay.

Let’s say you click through my site…the about résumé, my portfolio, and about me page. Web pages right? Wrong. These are resources! Take my résumé for example; if I printed it out on paper and handed it to you, it’d still contain everything you’d see if you would have accessed it on the web…but now it’s on paper! What if I took a screen capture and saved it as a JPG; or how about an Adobe PDF? Do you see where I’m going with this?

The “web page” you accessed was just one way to format the resource, in this case, my résumé.

The OTHER HTTP requests

Stick with me for a little while longer. Earlier, I mentioned there were eight types of HTTP requests, well here are all of the HTTP requests:

  1. HEAD
  2. GET
  3. POST
  4. PUT
  5. DELETE
  6. TRACE
  7. OPTIONS
  8. CONNECT

All I want is for you to be aware of these request types, I’m sure you already guess what a few of these do, but don’t freak out if you have no idea.

Let’s tie it all together

Let’s use our Bookstore application as an example. Now, what if I were to tell you this URL could do two different things:

  • http://localhost:3000/books/1

Say what? Do you remember what I said about HTTP requests?

Okay…imagine this…say you want to view book #1, when you click the above URL you are sending a HTTP “GET” request to the server; once the server gets your HTTP request it responds with the information you requested, in this case the title and description for book #1.

Well, after reading over the description I’ve decided book #1 sucks. I want to delete it. If you mouse over the “Destroy” URL for the bookstore app, you’ll notice the URL is as follows:

  • http://localhost:3000/books/1

Hey wait…that’s the same URL as the “Show” link! You are 100% correct. So how can one URL serve multiple purposes? You guessed it, the HTTP request!

When you click the “Destroy” link, you’re sending a HTTP “DELETE” request to the web server. When the web server gets your request it sees you want to DELETE book #1.

Crazy.

Take some time to let this all soak in, it’s actually quite important. You’ll see just how important this RESTful design is when we start digging into our Bookstore application.

You may have noticed I haven’t explained what REST actually stands for, honestly, I have no idea, does it really matter….probably not, but if you’re particularly curious, Wikipedia has the no-nonsense low down on everything we’ve discussed and then some.

I think this is a good point to take a break, not to mention it’s 11PM and I want to play Call of Duty 4. Recently, I added my blog to feedburner, if you want an instant update when I add a new post, subscribe! As usual, leave me a comment for better or worse…if you’re feeling like a badass you can even Digg this.

Until next time folks….

The next tutorial has been published: Rails’ Relationships

11 Responses to “RESTful design and the HTTP request”

  1. Basic Rail’s routing and a journey into Views, and Controllers | jonathan s ng Says:

    [...] next part of our journey through Rails covers RESTful design, an integral part of a properly designed and correctly executed Rails web application. in Ruby [...]

  2. 4eB Says:

    Haha, talk about a teaser! Followed all of your RoR tutorials up to this one - just wanted to leave a message to say thanks for doing such a clear, thorough job of these. There’s a dearth of v. 2 online resources for getting started in Rails, so it’s fantastic to see something like this available. :)

  3. R. Robot Says:

    Yeah, this is great, thanks for the friendly, no-jargon approach.

  4. Woecip Says:

    What a great tutorial. Thanks a lot for taking the time to do a step by step. The way you are presenting it is really giving my brain a chance to absorb what RoR is and how it can benefit me. As a PHP coder and old time **insert one of many prog. languages here** this is really a big , big help for me getting into RoR dev.

  5. Jaime Says:

    Thanks for taking the time to write this Jonhatan and please keep posting.

  6. James Says:

    Dude - I just wanted to say that I am thoroughly enjoying reading your tutorials - I want to be a sponge for all your knowledge, especially RoR knowledge :)
    Thanks again :)

  7. Mitch Says:

    Continue posting please. I love the way you explain these technical concepts.

  8. GonZoo Says:

    thanks a lot man, good work!

  9. Srikanth Says:

    Like the way you try to explain. Very clear tutorials..

  10. Dieter Says:

    I just can’t stop reading! ;-)

  11. Brian Says:

    very nice tutorials..hope we can learn more here..thanks!

Leave a Reply