Ruby on Rails CRUD Tutorial

Nancy Do
Nancy Do
Aug 13, 2018 · 5 min read

Table of Contents

What is Ruby on Rails?

Ruby on Rails (“RoR”) is an open-source web framework that is written in the Ruby programming language. It allows developers to quickly build powerful and robust web applications.

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 is designed to take the weight of endless configurations off the developer’s shoulders by including everything you need to get started when creating a web application. It allows you to write less code while at the same time accomplish more.


Ruby vs Ruby on Rails

Ruby is an interpreted scripting language for quick and easy object-oriented programming.

Ruby on Rails is a Ruby framework for implementing web applications. RoR depends on Ruby.


Rails Principles

MVC Architecture. The Model-View-Controller pattern is used to separate application’s concerns.

Source: https://onix-systems.com/
  • 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.

Convention over Configuration. Rails has a set of assumptions or defaults used for best practice for building web applications. This decreases the number of decisions developers have to make and allows for the implicit behaviors to set up the configurations for them.

DRY (Don’t Repeat Yourself). This guides the framework to reduce the amount of code you have to write, especially redundant code. This helps make your application more maintainable, extendable, and less buggy.

Benefits

Source: http://www.ragasoft.co.in/blog/ruby-on-rails-development/

Speed of Development. RoR minimizes the website development time by 25–50% as compared to other popular web frameworks.

Libraries. There’s a gem for just about anything you can think of. They are all publicly available at RubyGems.

Cost-effective. RoR is an open source solution, so unlike other commercial development frameworks there are no licensing costs involved.

Active RoR Community. RoR has an army of developers that are very open and friendly to beginners and offers useful plugins and libraries. The RoR community seems to truly care and want people to understand the benefits of Ruby on Rails.

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
  1. First, we’ll have to create a new Rails application (-T to create without the test framework):

2. Change your directory to access the application:

3. Generate the model for the application (model should be singular):

This creates 2 files:

  • app/models/dog.rb
  • db/migrate/[date_time]_create_dogs.rb

The dog table in our database will have 2 fields: name and motto.


4. Generate the controller for the application (controller should be plural):

Other than the “dogs_controller.rb” that gets created, a “dogs” folder along with four files we requested gets added within the views folder:

  • index.html.erb
  • show.html.erb
  • new.html.erb
  • edit.html.erb

5. The config/routes.rb file also gets automatically updated:

Since we are going to use all CRUD operations, let’s use the shortcut Rails has provided us. Update the config/routes.rb as such:


6. Now we’re ready to migrate our data. Double check our migration file to make sure it looks appropriate:

Run rake db:migrate . This will create the table in our SQLite database based on what is in our db/migrate/[date_time]_create_dogs.rb file.


7. Let’s add some dogs so we can run tests. Update the db/migrate/seeds.rb file with the following:

Run rake db:seed to add the dogs to the database.


8. In order to check if the dogs have been added to your database, run:

rails console or rails c

Type in Dog.all and you should see that the 4 dogs 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”. We’re going to be working in multiple files so to avoid confusion, I’ll bold and italicize the file you should make updates in.

9. CreateReadUpdateDelete
Goal: List all dogs

Controller — app/controllers/dogs_controller.rb, index method

Index — views/dogs/index.html.erb

Your page should look like this after running rails s or rails server.


10. CreateReadUpdateDelete
Goal: Show details of specific dog

Controller — app/controllers/dogs_controller.rb, show method

Show — views/dogs/show.html.erb


11. CreateReadUpdateDelete
Goal: Create a new dog

Controller — app/controllers/dogs_controller.rb, new and create methods

New — views/dogs/new.html.erb


12. CreateReadUpdateDelete
Goal: Update details for specific dog

Controller — app/controllers/dogs_controller.rb, edit and update methods

Edit — views/dogs/edit.html.erb


13. CreateReadUpdateDelete
Goal: Remove a dog from the database

Controller — app/controllers/dogs_controller.rb, destroy method

Show — views/dogs/show.html.erb
Add to your current code:


Here is the current Controller code:

You’ll notice that this doesn’t completely follow DRY because we have the same line of code to find the dog written 4 times.

14. Let’s refactor the Controller file:

That looks better!


15. Let’s add some more links to our index.erb file to connect all the other pages in one area.

Full Demo:


Troubleshooting

Understanding exactly what Rails is doing helps you know where to debug. Here is what happens when you click a button on your form:

  • The browser sends a request to the server
  • Rails gets the request and follows what method was listed (get, delete, post, etc.) and tries to match it with the appropriate route
  • Once it finds the correct route, it maps to the appropriate method in your controller and calls on that.
  • From there, it finds the associated view page (index, edit, show, and new) with that method to display the results to the user.

Don’t be afraid to use byebug!


Thanks for reading. Happy coding!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade