CRUD Basics
Greetings of the day!!!
In this article, you will see several concepts which are important for the design of Rails applications. These concepts will affect the way that we work with URLs, how we write controller code, and how our controllers manage our models and database data. We're gonna start by talking about the concept of CRUD. Let's begin with reviewing the MVC web architecture diagram. You must learned how to use controllers and views to create dynamic pages and return them back to the browser. Next, learned to interact with the database by creating models and associations.
| MVC Architecture |
And worked with those, not from a browser, but via the Rails console would be feasible. Now, we need to put the two together and learn how to work with the models inside our controllers. Then we'll be able to manipulate our database data from the browser, in the same way that we did from the console. The way that we're gonna work with the models and our controllers is using CRUD. CRUD is an acronym for create, read, update and delete. And those are four actions that are the main ways in which we interact with our models and with the database. Because these four are so common, in Rails we have standard controller actions for implementing them.
You don't have to use these standard action names but they are the Rails convention and life will be easier if we stick with them. For each element of the CRUD, there are two actions associated with it. Let's begin by looking at the top two lines, create. Create has two actions, new and create. New will display a web form which will be filled out with the data that we want to send to the database. Then when we submit that web form, it's gonna submit to the create action. Create will process the form and either save the information to the data base or handle any errors that come up.
So new displays the form, create processes the form. It's a two step process. Let's skip over read for a moment and go down to updatebecause it works very similar to create. The difference is now with update, there's existing data in the database. So it finds the existing data,we get a web form which is pre-populated with that data, we make any changes we want, we submit it to the update action, and the update action will process the form and submit those changes to the database. So edit displays the form, update processes the form.
And delete works the same way. Delete displays the information about the record we're about to delete. Destroy performs the destruction.The fourth part of CRUD, read, works a little bit differently. It does not have a display the form, process the form pairing. Instead it has two actions that are different. The first action is index, which returns a list of records. Show is an action which will display a single record, that is a detailed view of a single record in the database. So the first one is a list view, the second is a detail view.
Notice in the far right column of this table, there's a list of URLs which will be used to request these actions from the subject's controller. Most of these are self-explanatory. Index could include the word index after subjects but it's usually omitted and used as the default value. Notice that show, edit, update, delete and destroy all perform their work on an existing record. So they include the record ID in their URL. Index, new and create do not require an existing record so they do not. Together these eight actions are the core of working with CRUD in rails.
You will use them over and over again in every application that you build. Over the next two chapters, we're gonna learn how to implement them and use them to manage our subjects, pages, and sections in our CMS. I'll demonstrate how to add them for subjects and then later you'll have the opportunity to try adding them for pages and sections on your own. We're going to be dividing the CRUD for subjects, pages,and sections into three separate controllers. The subjects controller, the pages controller, and the sections controller. You typically have one controller per model.
It helps you to keep your code organized. And you're going to use plural names for your controllers. Notice it's subjects controller not subject controller because we are controlling all of our subjects with this controller not just one. This also allows for common sense URLs.For example, we will have subjects/new, pages/new, and sections/new, subjects/edit, pages/edit, and sections/edit and so on. These URLs clearly define the model which is being manipulated and the action which is being performed on it.
CHEERS,
Maitrey Patel
Maitrey Patel
Comments
Post a Comment