The RESTful Routes Sushi Bar: Breaking Down #new and #create in Rails
Tags: Forms, Rails, Software Development, Ruby on Rails, Ruby

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.

My favorite sushi joint in Houston: Ginza

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.

The typical sushi order 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.

#NEW

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.

My browser recreation of the sushi order form

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.