Create, Read, Update, Delete

CRUD — create, read, update, delete.

CRUD is used to describe the four basic functionalities of most any web application. From Facebook, to this silly game that I made, those four actions could be used to describe almost anything that is going on behind the scenes. I say behind the scenes, because of course there are other things going on for the user, such as HTML/CSS, and the use of JavaScript for DOM manipulation, but any time that we have to deal with data from an API, we will be doing some combination of creating new records, reading saved records to display to the user, updating a record with new information, and lastly, deleting a record so that it can no longer be accessed.

Recently, I was looking back on projects of mine that weren’t necessarily “completed”, but at the same time had some functionality, and I had discontinued working on them. I realized that I had never included anything related to the big “U.” I rationalized this by thinking that of those four described basic functionalities that “update” is the least important. I still think the same thing, but still, it’s CRUD, not CRD, and so I set out to find at least one project of mine where I can implement the functionality to update a record.

There are two types of records that come to mind for me when I think of updating — profile informations, and user posts. Updating profile information would allow a user to change contact information, or reset a password, and a user might want to update a post they’ve made if there was a mistake while making it, but not so big of a mistake that it would warrant deleting the post altogether. I chose to work on a project with user profiles, specifically, I decided that I would begin by adding in the ability for a user to update their user name.

Inside of my Rails back end, I already had the appropriate route written since I had used the command “rails g resource Users” as I was starting out with the project — side note: remove routes that are not in use — so the route looks very similar to that which I would use for a show method, with one significant difference: the HTTP verb. To utilize the update route that had been so kindly generated by the rails command, the verb that I would need to use would be either “Put,” or “Patch.” These two options both serve to update a record, however they differ in their methods; “Put” creates a completely new record with the new information, and then deletes the now outdated record, whereas “Patch” simply updates the existing record. To me, the “Patch” option seems objectively better, however from what I have seen while researching, both options will work well for the average developer, and seem to be about equal in terms of performance — I still prefer “Patch.”

Using the React front end that I have also set up, I generated a JSON object to send via the “Patch” request, and all that is left to do is write up the update method in my users_controller.rb. This update method can be easy to mess up, so I suggest starting in the Rails docs, and I’ve also found that using the command “rails console — — sandbox” which allows me to mess around with my methods and data in a console session, but the data is reverted back to its original condition once I end the console.

Having implementing the fabled Full CRUD in my application, I feel satisfied, but I think there is still more basic things that can be found in most applications, but not mine — wouldn’t it be nice to have the option to reset the password by email/phone?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store