Create, Read, Update, Delete
CRUD — create, read, update, delete.
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?