How to create a simple CRUD app using Rails!
I have been developing websites using CMS’s such as WordPress and not knowing how WordPress is working behind the door was frustrating especially if I wanted to make some core changes. After I start coding Bootcamp at Flatiron School, however, I feel empowered to be able to create a full-fledged databased supported CRUD (Create, Read, Update, Delete) app using rails. On this blog, I will try to demonstrate how versatile Rails is and how Rails make it easy to create a simple CRUD app.
The Test app will have one domain “Country” with attributes of “Name, “CapitalCity” and “Population”. In the end, the app should be able to create a new country, list all the counties, display a Country with the Capital City and Population, edit a country and finally delete one.
The first step is to make a new rails app.
I will now create the model using rails resources. There are multiple ways to create the models in Rails varying from Rails Scaffold in which it creates everything including some functionalities our app might not require to Rails Model new which creates only the Model file. Rails default field type is a string so we do not need to write string for a string type attribute.
Rails resources will create the model Country with its attributes, the Controller, the view directory for the model and the database migration.
The next step would be to migrate our database. Depending on the database you are using the next step might differ. I am using PostgreSQL and I first need to run rails db:create first and rails db:migrate next.
To make sure if our database is migrated we need to check the schema file found on the db folder.
At this stage, the database is set up so I will start working on the CRUD aspect of the app.
- Add a country — the route is set to resources :countries, meaning that all the 7 CRUD routes are enabled so no need to make changes in the routes file.
To be able to add a new country we have to write the new method in the CountiesController.
To get the information required to add a country to our database we need a form. To create the form I will use Rails form helpers, you can visit https://devhints.io/rails-forms to learn more about the helpers.
The information submitted through the form can now be accessed from the URL through a special hash called Params. The params come from the user’s browser when they request the page. To learn more about Params visit https://flatironschool.com/blog/how-params-works-in-rails/ .
To be able to receive and process the information provided from the users from the forms we need to write another definition “Create” in the controller.
Before creating the new model, however, there is a security procedure “Strong Params” in Rails that would permit the attributes that would create or edit a model, it is only after the attributes are permitted that the instance is added to the database. We can create a method in the controller to require and permit the attributes.
Now after the attributed are permitted we can create a new instance of the Country model.
2. List all countries — Now we are able to create instances of the countries let’s list all counties in the database in the index page. The index page will be accessed through http://localhost:3000/countries .
First, we need a new method in the counties controller called index. We will store all the countries in an array.
I stored all countries from my database as an array in the @countries variable.
Now at the index.html.erb template file I iterated over the array of countries and displayed the name, capital city and population of the counties. I used the link_to rails form method to link a County name to its page display. We will create the display/show page of a single country next.
3. Display a Single country — To be able to display information of a single country we have to define a new method called “Show” in our controller.
I need a template page to show information for a particular country. According to Rails conventions, the name of the fine should be show.html.erb.
The @country variable is checking the id from the URL to search a specific country from the database and supply it to the template. Knowing the exact county, the template now is able to access the information through the attributes.
4. Edit a Country — What if we want to edit a specific country? Rails make it easy to look for a specific country, show all the information stored in the database and update that specific country. To achieve this, first, we have to find the specific county to edit. The Rails method to provide the form to update an instance is “Edit”. We will call the method in the Controller file and make the necessary corrections.
Let’s now create a form to be able to update.
Rails is smart enough to figure out the form is to edit that specific country and displays the Country’s information from the database.
After the edited for is submitted we need have a method in the Controle to process the data and update the database. Rails have a method for this called “Update”.
5. Delete — The delete page does not necessarily need its own template page. It can be added to the show page of a specific country page as a button. Rails has a button method called “button_to” that takes a method to achieve a particular action. In this case, we will use the delete method to ab able to delete a specific country from the database.
Following Rails convention, we need to set up a method at the controller page that would process the information and perform the required task against the database. We need a method called “Destroy” at the controller page that communicates with the delete method from the show template and destroys the instance supplied via the form.
After the task is accomplished and the country is deleted from the database the method will redirect to the counties list since the country in which the button resided does not exist anymore.
This is part of the knowledge I have acquired so far in the first month of the tech Bootcamp and I look forward to what I will be learning in the remaining two and half months.