Wednesday, January 28, 2015

RESTful Routes II: New/Create

Previously on RESTful Routes Part I, 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).

The cliffhanger at the end of last episode was that there are a bunch more RESTful actions than there are CRUD...things. Why wouldn't they have same amount, like one REST thing takes care of one CRUD thing. Boom.

Let's go back to the actions again. Some of these seem like they're synonymous - why would there be both New and Create, for instance? Surpise! They actually do slightly different things. Check it out:

Say you want to sign up as a new user for a site about kittens riding ponies. In order for that to happen, you have to do the actual signing up, the entering of info and the clicking of "save"? But you also have to navigate to that page in the first place - try signing up for something on the internets when there's no sign in page. You would have to just write on the screen with a Sharpie, and then take a picture of it, and then tie that picture to the leg of a pigeon, and then have that pigeon fly to the headquarters of kittensonponies.com.

So, we have established the need for a reachable signup page, and the need for filling out the sign up page. New and Create, respectively, address those needs.

New isn't the process of making a new entry for the database, but rather brings to the place where you can begin to make that entry. All it has to do is register that you just clicked the "new" or "sign up" link, and take you to the page where you can do that. New is a GET action, because you're fetching, or GETting, the right view.

Create, then, is the action that makes the new object (which is fancy programmer talk for "your new user profile" in this case). There are a few parts to this action. In order for this all to work out the way we expect it to, three things must happen:
  • The information for the object gets validated and saved in the database. By "validate", I mean that the information in the object meets predefined criteria - the password doesn't have a special character, or the name field is blank, for example. 
  • The user receives feedback if the validation fails, including the reason for the failure. 
  • The user is taken to the right page upon successful submission. So, does it take you to your new user profile, for instance. Create is a POST action, because you're posting new information, or a new object, where before  there was none.


Apropos of nothing. I just felt you should have some cute. Because I care.



No comments:

Post a Comment