What the CRUD?!
How is CRUD important in full stack web development? CRUD, is the code we as developers write in our web application so the user can perform CRUD actions in our web application. As developers we write our code so the database can store our application & users information. Almost, every web application involves CRUD.
What is CRUD?
- Create
- Read
- Update
- Delete
Every web application needs a framework software. Enter Rails.
Rails is a web application development framework written in the Ruby programming language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Experienced Rails developers also report that it makes web application development more fun.
Rails provides developers with RESTful actions that pair along with a CRUD element. RESTful — stands for REpresentational State Transfer. This provides a consistent pattern to use in making web applications url routes.
Rails provides you with 7 RESTful actions to simplify your CRUD code.
- New
- Create
- Index
- Show
- Edit
- Update
- Destroy
Here I will outline how each CRUD action works with the user in our application along side Rails.
CREATE
- User: creates a new post
- New URL Route: http://www.ourblog.com/new
- Rails: 1) The new action renders the new.html.erb page that user sees in the browser and is able to create a post. 2) The create action happens once the user clicks the submit button & database is given commands to create a new instance & save itself to the database. 3) The user is then redirected to READ their post.
READ
- User: searches for a list of all posts
- Index URL Route: http://www.ourblog.com/posts
- Rails: Index action will render index.html.erb page in the browser, the database will query for a list of all posts & return the list to the user in the browser.
- User: selects to read a specific post
- Show URL Route: http://www.ourblog.com/posts/:id
- Rails: Show action will render show.html.erb page. The database will find the specific post by it’s [:id] in the database & send the post back the browser to display. Then the user can choose to UPDATE.
UPDATE
- User: can choose to do updates to their post(s)
- Edit URL Route: http://www.ourblog.com/posts/:id/edit
- Rails: 1) Edit action renders edit.html.erb page. The user then edits the post. The user then has two options, they can decide to UPDATE the post or DESTROY the post (see below for destroy). 2) Update action is then followed to the database to match the post & update the changes in the database. 3) Upon completion the user will be redirected to the show action to view the changes of that post [:id].
Destroy
- User: deletes their post(s)
- Destroy URL Route: http://www.ourblog.com/posts/:id/edit
- Rails: 1) Destory action will render on the edit.html.erb page as a button. If the user clicks the button the destory action would find the the post by [:id] and destroy all the post tied to that [:id] in the database. 2) The user would then be redirected to the index.html.erb page to see the updated list of all posts.
That’s a wrap! I hope you have a better understanding of CRUD & how Rails works with CRUD. Here is a great Rails CRUD tutorial.