A Beginner?s Guide
When I first started learning to build web applications, I remember thinking ?What do I build?? and this is normal. Then I learned about CRUD, which essentially is the basic functionality of an application. Specifically it deals with Creating, Reading, Updating, and Deleting records within your application. After learning about this concept, it helped me to build a base of what functionality I could add to any app I built. In this article, I?ll breakdown the four key concepts to CRUD and why it?s beneficial to learn about it.
Let?s dive into the details. CRUD is basic functionality that we want our models to possess in our applications. So if we?re building an API that has a Recipe model, then that model should be able to Create new recipes, Read (or Show) those recipes, Update (and Edit) them, and lastly we should be able to Delete them. Seems pretty straight forward and honestly it is! Let?s see what these individual functions look like.
For the sake of continuity let?s continue with our Recipe model.
Obviously our app should be able to create new recipes and that?s the main goal of this function. We would perform this type of action by using a form that contains inputs which correlate to the model table in our database. When we hit the submit button for our form, then a POST HTTP request would be sent to our API and add this new Recipe into the DB.
Cool so we?ve made our Recipe but we want to see it now! Read is our main functionality for this and it would consist of a function that queries our database for our specific recipe we just created OR we could ask to see all the Recipes available. To pull an individual Recipe we should use a unique attribute of the model record such as the Recipe ID. The key thing to remember is that we don?t want to modify the record at all but simply retrieve and view it.
We?re looking at our new Recipe and realize that something is missing! How do we expect to fix this? Well now we need to update this recipe and part of this is Editing the record before Updating it. To get this functionality we would have a form similar to our original Create form but this time it has filled in fields which we can now change. Here we would Edit the recipe to match what we want and then on hitting submit, we would have an Update action that creates a POST HTTP request to update the record in the database with new info.
A bunch of recipes have been created and that?s super exciting but we notice that one of them we no longer care for, what do we do? we DESTROY it! Okay maybe it?s not that intense but this is correct. The last step in CRUD functionality is Deleting or Destroying a particular record we no longer need. This can take the shape of a delete button or an ?X? next to our recipe that then triggers a DELETE HTTP request that informs our database to remove our recipe.
All of these actions seem simple in concept but they add very important functionality to manipulating your models. These actions are vital to modern day web development and once you?ve built up familiarity with building it out, you?ll find ways to get more creative in what you can do.
As a new developer, I encourage you to build your apps around this basic functionality. It can be applied to both frontend or backend frameworks and when creating full-stack applications, you?ll find yourself having to plan how your stack will communicate the CRUD actions between the client and server.
I?ve added some additional resources at the end to help provide further details in your studies!