Using Full C.R.U.D inside a Rails App
I want to talk about something that I enjoyed learning to do while creating my most recent project. Handling data was all I thought about for two weeks straight! So here is an example of using full CRUD with Active Record in a Ruby on Rails application.
What is Ruby on Rails?
Ruby on Rails is an open-source web framework that’s written from the Ruby programming language. It allows developers to build very powerful and rich web applications. Rails was designed to be quicker, and to already include everything you need to get started when creating a web application. It lets us developers write less code, save more time, and accomplish what we need to do!
Rails Principles
MVC Architecture. The Model-View-Controller pattern is used to separate application’s concerns.
- Models — Ruby classes that handle the business logic by talking with the database to validate data
- Views — Templates (typically formed of a mix of HTML and Ruby code) that render the data from your models and handle logic for presentation that end users see and interact with
- Controller — The middleman between the model and the view controlling the flow of the application. They handle the requests, initiates changes to the model, redirects, etc.
CRUD Tutorial
In this tutorial we will create a Rails CRUD application and briefly demonstrate how Rails communicates with a SQLite database.
CRUD operations are commonly used in web development and it stands for:
- Create
- Read
- Update
- Delete
- First, we’ll have to create a new Rails application
rails new league-of-legends-spell-maker
2. Change your directory to access the application:
cd league-of-legends-spell-maker/
3. Generate the model for the application (model should be singular):
rails g model ability name description
This creates 2 files:
- app/models/ability.rb
- db/migrate/[date_time]_create_abilities.rb
The ability table in our database will have 2 fields: name and description.
4. Generate the controller for the application (controller should be plural):
rails g controller abilities
5. Since we are going to use all CRUD operations, let’s use a Rails shortcut. Update the config/routes.rb as such:
(Note: I added a “manual” patch on line 3 because I don’t need any other kind of “update” action like PUT)
6. Now we’re ready to migrate our data. Double check our migration file to make sure it looks appropriate:
Run rails db:migrate
. This will create the table in our SQLite database based on what is in our db/migrate/[date_time]_create_abilities.rb file.
7. I added some abilities so we can run tests. Update the db/migrate/seeds.rb file with the following:
Ability.create([{name: "WORLD ENDER",
description: "Fear nearby enemy minions and gain attack damage, increased healing, and movement speed. Takedowns extend this effect."},{name: "STATIC FIELD",
description: "Enemies attacked are marked and take lightning damage after 1 second. Additionally, activate this ability to remove nearby enemies' shields, damage them, and silence them briefly."},{name: "ACE IN THE HOLE",
description: "Deal massive damage to a single target at a huge range. Enemy champions can intercept the bullet for their ally."}]);
Run rails db:seed
to add the abilities to the database.
8. In order to check if the abilities have been added to your database, run:
rails console
or rails c
Type in Ability.all
and you should see that the abilities have been added.
FYI. Run rails routes
or go to localhost:3000/rails/info/routes to see all routes your application is configured to:
Now we’re ready to start working on our CRUD functions. Let’s start with “Read” and work our way down to “Destroy” and our useful private methods!
9. Read(INDEX)
Goal: List all Abilities
Controller — app/controllers/abilities_controller.rb, show method
10. Read(SHOW)
Goal: Show a specific ability
Controller — app/controllers/abilities_controller.rb, show method
11. Create(CREATE)
Goal: Create a new ability
Controller — app/controllers/abilities_controller.rb, new and create methods
12. Update
Goal: Update details for specific ability
Controller — app/controllers/abilities_controller.rb, update method
13. Delete
Goal: Remove a ability from the database
Controller — app/controllers/abilities_controller.rb, destroy method
You’ll notice that in some of our methods we have the same line:
ability = find_ability
This became pretty repetitive, so use a private helper method to DRY things up!
Add to your current code:
Also the ‘find_ability’ is in our CREATE and UPDATE methods. Why not save a few lines of code?