Wednesday, January 7, 2015

RESTful Routes Part I: An Introduction, or This Is What We Talk About At Breakfast

Once upon a time, over biscuits and gravy and breakfast sandwich, respectively, Mentor Whitney and I discussed Controllers, CRUD, and REST architecture. I will share the fruits of this discussion with you now. I will not share with you the fruit from my breakfast. Because I ate it.

So, what am I even talking about? Be patient, gentle reader. The mystery will unfold in its time.

Remember from the post about MVC architecture that a controller is like the kitchen manager, making sure orders get sent to the chefs, taking the food requested from one of those orders and making sure that gets to the folks who do the fancy plating, and sending the finished meal out to the customer. Outside of the metaphor, in an actual application, the controller takes in requests to the database through the model, brings responses to the right view, and sends the right info and view to the user.

Now, imagine your restaurant is super weird - a farm-to-table Nebraska Halloween themed restaurant, perhaps. That is to say, imagine your restaurant is in the middle of a corn maze (aside: best corn maze EVER) and all the parts are in different clearings in the maze*. Customers order over here, chefs and walk-in freezers are over there, and the folks who do the plating are way over there. Can you imagine what it would be like for your kitchen manager if, on their first day, you said, "Hey, can you go give this order to the chefs?" but you didn't tell them how to get there? If your kitchen manager was anything like me, they would probably be like this:

Controllers are people, too.

Just like that kitchen manager is gonna freak out if you give them this impossible task, your controller will, too, and it will give you the error message equivalent of the above.

This is where routes come in. Routes are how you tell your kitchen manager how to get to all the places in the restaurant. Let's check out a visual real quick. If you get yourself into a rails project in the terminal window, and then run the command rake routes, you'll get something similar to this (it will vary depending on how you have set up your app):

Before we talk more about that table, let me throw a couple of acronyms at you. Don't freak. The first is REST, like in the title of this post. REST stands for Representational State Transfer, and at this point, we just need to know that REST is an architecture style that makes it possible for Things internal and external to your application to talk to each other and get the user what they ask for.

The scope of what the user is going to ask for can be summarized by acronym #2: CRUD. CRUD, though it sounds a bit vulgar (especially if you're British), but really just stands for Create, Read, Update, Destroy. This is most of what we want to do on the internet, right? We want to create things (like a user account, or a blog post, say), see and show them, be able to edit them, and delete them. Basically, we use RESTful architecture to carry out CRUD.

This series of posts is going to talk about how each part of CRUD gets handled by RESTful routes. For now, let's just take note of the "Verb" column above, and the "Controller#Action" column, and then we'll be done for now.

Those Verbs are actually HTTP verbs - the verbs that your browser uses to retrieve and show pages, etc. The four main verbs are POST, GET, PUT, and DELETE (though you can see PATCH up there as well), and these correspond to respective CRUD operations.

The first part of the "Controller#Action" column tells you what controller you're talking about (here we only have one, but we'll need more). The last part of this column gives the name of an action. The specifics of these RESTful actions are defined in your controller, but there are seven types overall: New, Create, Edit, Update, Index, Show, Destroy. 

Super complicated math tells us that we have four CRUD operations, four main HTTP verbs, actions? Whaaaaa? We'll get into this voodoo magic next time, on...This Blog.

This is totally you, waiting for the rest of the posts in this series.

*Ooh! Ooh! OR it's an M. Night Shymalan themed restaurant set in the middle of some crop circles.

No comments:

Post a Comment