Monday, March 2, 2015

RESTful Routes III: Final Reckoning

Previously on RESTful Routes I and II, we discussed HTTP verbs (get, put, post, delete), and RESTful actions (new, create, edit, update, index, show, destroy). Now we're going to check out how those things fit together to carry out CRUD (create, read, update, destroy).

When we last saw our hero, RESTy McRouterpants, we explored the difference between the New and Create actions, using the example of setting up a user profile on the omg-I-wish-this-existed website, kittensonponies.com. In a nutshell, New brings you to a sign up page (for example), and Create makes the user profile.

Our remaining actions to discuss are Edit, Update, Index, Show, and Destroy.

Let's begin.

Edit:

If we're thinking about the hypothetical user profile, you might look at the Edit action and think "Why, I bet that means to edit something." But you would be wrong. Or at least, only a little right. Edit is like New in that it sets the user up to, or gets the user to a place where they can make changes to their profile. In order to this, like New, Edit needs to be able to get you to the right view. You can't make changes to your profile if you're not on the edit profile page. You just can't. Unless you want to put white-out on your screen (Mom, if you're reading this, DO NOT PUT WHITE-OUT ON YOUR SCREEN).

Edit also is in charge of identifying the right user profile to make changes to - imagine if you go to edit your profile, and kittensonponies.com brings you Fidel Castro's profile to edit (because you know that Fidel Castro loves kittens on ponies). *Awkward*. Plus, that might make the FBI to open a file on you.

Update:

Update, then, is to Edit what Create is to New. Edit leaves the user poised to make changes, and Update takes care of the rest. The Update action makes sure that user inputs are applied to the right object within the profile (e.g. if you're changing to Fidel's email address, it makes sure that doesn't change Fidel's name instead). Update is also in charge of noting the success or failure of your changes, once you've clicked submit (e.g. if you change Fidel's address to "fidelluvsginuwine@cuba"*, it will fail for lack of a .com, but will succeed once you fix it). Update's final job is to get you to the right view, like the newly edited profile page, for instance.

Index:

Now let's say the kittens of kittensandponies.com have profiles where they make blog posts** about their equine escapades.The Index actions gets all the objects (blog posts). To do that, Index must be able to get the right template and the right array of objects - together the template and objects compose the appropriate view.


Show:


Let's say you're perusing the blog of Oscar the tiny orange kitten...

Wake up, kitty! Horseback riding is dangerous!

...and you come across Oscar's account of the day he rode a Clydesdale, and you absolutely have to read it. Enter the Show action. Show shows you that single post. To do this, it must get the correct view template, and the correct object to insert into that template.


Destroy:

Now, if Oscar got really embarrassed about his poor spelling ("Klydzdayel"), and just wanted to delete that post, he would call upon the Destroy action. Destroy would get the right object (because Oscar does not want to delete the post about the Appaloosa, because he spelled it right and he's psyched about it). Then Destroy would remove that post from the database, and take you to the right view (like the index, perhaps) after.

As for me, Destroy is a little bit anti-climatic. I feel like it should involve explosions. I don't want that kind of disappointment for you, so I've included the following to punch it up for you a little bit.





*y'know...because Ginuwine sings the "ride it, my pony" song. GET IT?!?!

**"Dear Diary, today I rode the most beautiful Appaloosa, dappled to perfection..."


1 comment: