Implementing CRUD Using Ruby On Rails CLI
Learning to be a Full-Stack web developer
– a remote software development school domiciled in California had so much taught me a lot like patience
and the curiosity
to get things done the right way and at the right time. Ruby on rails web application is an application that is somewhat complicated for someone who is getting started in learning how this powerful web application works. But when you get to understand how it works and how to navigate round in building your application with it, you will be glad you did.
In this article, we will be looking at how to create, edit/update, delete and validate an article from the back-end of your ruby on rails application using the console. The same thing we achieve using the console can as well be done by the user of the application from the front-end but we won’t be covering that in this article but we will mostly focus on doing that from the back-end using the rails console.
I will be sharing examples with us in this quick tutorial from one of the projects I have worked on so far so as to show us how this whole thing works. Create your application by running the rails command $ rails new
(anything you want to call your application), i called mine omega-app
. Be sure to have your gemfile
file working properly. Feel free to check my article on that here
in order to get to see how to properly make your gemfile
and database file to be in the right order to make your application work.
In your pages folder from your views
location, add your pages file to your own discretion and their corresponding routes that link to each of the pages you would have. Here is an example of how your home.html.erb
file will look like and its routes.
You can create other pages, add their routes and what you have as the content is totally at your own discretion. You can as well add an inline css styling here. Make sure to follow the normal html page format convention. Here is a quick view below of the routes from this article.
Also have a look at the pages controller,
Alright with that said, run this migration command in your command line in order to generate a migration in your application $ rails generate migration create_articles and $ rails db:migrate so as to create your table and have it migrated properly. Your migration file from your db – database folder should look like this below:
Again run the command $ rails generate migration add_description_to_articles and it will create a new migration file. Then add this to your just migrated new file from your second migration table below and then run $ rails db:migrate command.
Your schema.rb file will look like this below after running your $ rails db:migrate command as instructed above.
It now has a table that has a title, description, created_at and updated_at. But a model is needed so we can communicate with the database. So in the models folder, create a file article.rb that would look like this below without any validation which we will add later.
With the models now created, rails now give us getter and setter for the table, i.e. for each attribute in the table. We can now proceed to the console so as to check out what we have been working on. Rails console gives us direct access to the database. The console can be a playground where you can test anything, methods, connection to the database etc.
The command $ rails console will take you to the console. So in the console, article = Article.new command will show you something like this below, nothing has been created in the new article table.
To Create An Article
So, to create a new article simply say: article.title = “This is my article for sure” (remember you can use anything) and press the enter button. When you type article, you will see that the title space has been filled leaving the description space still at nil. For the description, type article.description = “This is my next description” and press enter. To check for that, type article and observe how the description column has been filled up
Created_at, updated_at and the id would be handled by rails, what this means is that rails will provide the information about the id number of the article, when it was created and what time it was updated. So, to save type at the console article.save. Rails would generate a sequel query that would handle the transaction which had updated, created and added our id as we can see from the pix below which is committed to the table by rails.
The command Article.all will show all the data that has been created in the database.
This whole thing we did here can be done in just one line of command which is known as mass assignment instead of doing it one after the other as we just did above. To do this, create a new article variable as article = Article.new(title: “This is my article for sure again”, description: “This is my next description again”), article.save command saves the information to the database.
Rails create method creates an article that directly gets to the database without the developer having to save after creating the article.
So Article.all shows all the articles that have been created so far. There are seven articles from our table here which we have in all including the one I have added from my previous work. So the one we just created started from article no 5 from our database.
To Edit/Update An Article
To edit or update an article, type article = Article.find(7). What this means is that you want to grab article number seven or anyone you want and then press enter. Type in article and it will take the article number seven. So supposing we want to edit or update the article title or description of the article, we can type either of it in either of this way: for either title or description: article.title = “I need to change this article title” and press enter and then type article.save to save it. When you run the command Article.all, you will observe from the article id no 7 that the title has changed.
So that’s how to edit or update an article title from the console and same can be done for the description.
To Destroy An article
We can as well follow the same method we followed while trying to edit an article to achieve the same thing when trying to destroy an article. For example: article = Article.find(7), remember you can use any article id you want. Follow the normal process as we had it in the edit steps but use article.destroy command to remove the article from the article table. Run the command Article.all and see that the article has been removed from the article table.
To maintain data standard, we need to add some validations to our article.rb file so that we can be able to create a data that has a title, some description and other information like minimum and maximum length of characters. If not we will create data in the database without title and description. So from the information we have below, fill in your article.rb file with all the validation information as described. Based on your experience with Ruby On Rails, you can add more validations depending on what you want your application to do.
So what this validation information will do is to make sure that your article title and description maintains the standard of what it has been assigned to do. Your article title will not be more than 50 characters maximum while the minimum will not be less than 3 characters. So the same thing goes for the description.
Try creating an article without title or description and see the error message or try to use characters against what is required from the validation and see the result.
But make sure to reload your console with the command reload! Or exit the console and start all over. Check the error messages using the command article.errors.any? and type this command next: article.errors.full_messages and get to get the error message that will be given to you.
And so you are done! The validation is working. You have been able to create, edit/update, delete/destroy and validate information in your database from the console in your Ruby On Rails application.
Finally, commit your work using the git command $ git add ., $ git commit –m “(your commit message)” and $ git push origin (name of your branch). Hope you found this article very helpful. Thanks for taking your time to read the article.
Kingsley McSimon Ogbonna – Full-Stack web developer student at Microverse – a remote software development school based in California.
You can check my Portfolio, Connect with me on LinkedIn and follow me on Twitter
Subscribe to get your daily round-up of top tech stories!