The RESTful Routes Sushi Bar: Breaking Down #new and #create in Rails
Recently, I began my journey learning the ins and outs of Rails. I felt like I had gotten a basic mastery of Ruby so far, but when I first saw Rails in the flesh I just…didn’t get it. I understood the basic mechanics and how to accomplish tasks, but I couldn’t understand WHY I was doing those things. Peeling back another layer of abstraction was a big challenge for me.
Luckily, the fog has cleared (a little bit) and today I am going to explain a couple of RESTful routes in Rails that I was having trouble understanding at first: #new and #create. The two work hand in hand, and if one is being used the other is essential. They accomplish one primary task: creating a new instance of an Object.
When you are using a form to create a new instance from your web browser, it’s a little challenging to comprehend what’s going on behind the scenes. So, I have started relating the whole process to something we all may be familiar with: ordering at an old-school sushi restaurant.
You all know the one I’m talking about. It’s been in your neighborhood for decades and hasn’t been renovated in 30 years. Forget the fancy cocktails and the elaborate sushi rolls… you’re getting Sapporo and a California roll. As a kid, my favorite part about the local sushi place was filling out the SUSHI FORM.
If you haven’t used one of these before, it’s pretty self explanatory. You tick off however many of each sushi roll you want in the corresponding box. The waitress picks up the form, confirms your order, and takes it over to the chef.
Now, let’s set up this analogy: imagine you, the diner, are the client computer and you are sitting down at a restaurant, the server computer. Let’s say your sushi order is actually the beginning of a new instance of the Sushi object. When you sit down, you, of course, want to order some sushi. The empty Sushi form that is sitting on your table is exactly like going to sushi/new: it is the blank form for you to fill out.
You fill out the form, and hand it over to your waitress (press submit on the form in the browser). Then, the waitress takes the form over to the chef and begins the CREATION of the Sushi. This, not surprisingly, is the beginning of the create action within your SushiController! (does Rails know that the plural of sushi is just sushi? It may be magical but it is seriously lacking in its grammar skills.)
The part that didn’t make sense to me at first was…how does Rails know to go to the #create action after we press the submit button? Luckily, this is part of the RESTful conventions that Rails uses…it implicitly knows that when the submit button on a #new form is clicked, then the data in the form needs to be collected and taken to the #create action. We don’t have to explicitly say so within our code…true Rails magic. Just like when the waitress gives the form to the chef: she doesn’t have to say to him “Here is an order for sushi. Create this sushi.” She gives him the form and he knows that the form is for creating the sushi.
Now, for the Sushi creation process. The chef rolls out the seaweed and the rice (creating a new Sushi object). Then, he tops it with crab, avocado, eel, or whatever is needed to make this particular roll. To me, this is like adding the different parameters in the roll: the ingredients attributes!
After the sushi chef is done adding ingredients, he rolls it up, slices, and plates it, essentially “saving” and finishing the creation process of Sushi.
Now, what’s next? The sushi chef doesn’t know what to do. He didn’t take the order, so he has no idea who ordered this roll and where it belongs.
This is where the waitress/redirector comes in to play. The waitress takes the newly created Sushi from the kitchen, and brings it back to you, the person who filled out that Sushi form in the first place! Similarly, at the end of each #create method, it is crucial to add the redirect_to helper method, which usually redirects to the #show view page of the newly created Object. That way, the person using your application can see for themselves whether or not their new Sushi object was created (or they could receive validation errors, but that’s for another blog post…).
So, in many ways, the process of creating a new object in an application is a 3 step process: #new (which contains the form a client will fill out), #create (the actual creation of this new object), and #show (redirecting the client to their newly created object so they can see it for themselves).
The good news is if you understand #new and #create, you already understand #edit and #update! Similarly, they both go hand in hand in the process of updating the attributes of an object.
I hope this helped you understand some crucial RESTful methods a little better! Now, the moment we’ve all been waiting for: cats dressed as sushi.