How to Create a Ruby on Rails App
In this short tutorial, I would like to walk you through essential steps to build a Ruby on Rails Application.
Here is what Wiki tells us about Ruby on Rails:
Ruby on Rails, or Rails, is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as JSON or XML for data transfer and HTML, CSS and JavaScript for user interfacing. In addition to MVC, Rails emphasizes the use of other well-known software engineering patterns and paradigms, including convention over configuration (CoC), don’t repeat yourself (DRY), and the active record pattern.
Wow! This sounds like a lot! Simply speaking, Rails is a web-application framework that includes everything you need to create database-backed web applications according to the Model-View-Controller (MVC) pattern. Understanding this MVC model is key to understanding Ruby on Rails. MVC divides your application into three main layers: Model, View, and Controller, each with a specific responsibility. I am going to demonstrate how to create these elements in practice. Please feel free to code along.
In addition, if you want to learn more about Rails, here are a couple of good resources for that:
All right, let’s get started! I am going to write a simple bookshop application that is going to have two tables: books
and authors
with the following relationships: any author can have many books (has_many :books
type of relationships in Ruby terminology) and any book can belong to just one author (belongs_to :author
type of relationships in Ruby terminology). After completing this application, you will be able to render in a browser all the books, authors from a database. Keep in mind; I will focus only on backend portion of the application.
Install a database
Before creating the application, we need to ensure that we have a database to store data. We can use different types of databases but Rails is configured to use SQLite by default, and for the sake of simplicity, let’s move forward with SQLite database. Depending on your platform (Linux/Win/Mac OS), different steps are required to set up and install your database; I will skip these steps here, since there are tons of guides on that out there in the internet. As a reminder, don’t forget to configure your SQLite Database.
Create a new project
With the database installed on your local machine, we can create a new Rails project. Let’s start by creating a simple book-shop app in Rails. Please keep in mind that these steps below can be reproduced by applying different commands, since Rails provides a lot of useful tools / commands to create main components of the application, more on that here
To set up and create the app, run the following command:
rails new book-shop — skip-javascript
Then create an author
model that has two attributes name and country. The command below creates a new migration, an author
model that we need to populate with has_many :books
relationship.
rails generate model Author name:string country:string


Next step, create a book
model that has seven attributes. The command below creates a new migration, book
model that we need to populate with belongs_to :author
relationship.
rails generate model Book title:string description:string publisher:string language:string price:float length_page:integer published:boolean


After that, we need to set up relationship between both tables by adding a foreign key attribute to the book migration, to do that, please run the code below. That links attributes of the books table to the authors table via author_id column in the books table.
rails g migration AddAuthorToBooks author:references

Run rails db:migrate
command to run change or up methods for all the migrations that have not yet been run.
Once we have both tables in place, we need to populate them with data. For the sake of simplicity, let’s populate both tables manually, copy and paste the code below on your seed.rb
file and then run rails db:seed
command. It populates the tables we created before. If you want to learn more about a Ruby on Rails File Structure, here is a guide I wrote before.
Author.create(name: "William Shakespeare", country: "England")
Author.create(name: "Eiichiro Oda", country: "Japan")
Author.create(name: "Leo Tolstoy", country: "Russia")
Author.create(name: "Karl May", country: "Germany")
Author.create(name: "Gerard de Villiers", country: "France")
Author.create(name: "Agatha Christie", country: "England")
Author.create(name: "Alex Pushkin", country: "Russia")Book.create(title: "Some Book", description: "Interesting", publisher: "XYZ", language: "Englisg", price: 10.00 , length_page: 123, published: true, author_id: 1)Book.create(title: "Super Book", description: "Interesting and long", publisher: "ZZZ", language: "Japanese", price: 25.00 , length_page: 457, published: true, author_id: 2)Book.create(title: "Interesting Book", description: "Good and mysterious", publisher: "SSS", language: "Russian", price: 99.00 , length_page: 520, published: false, author_id: 3)Book.create(title: "Long Book", description: "Fascinating", publisher: "SomeCompany", language: "German", price: 13.00 , length_page: 50, published: true, author_id: 4)Book.create(title: "Short Book", description: "Jaw dropping", publisher: "BookLLC", language: "French", price: 25.00 , length_page: 999, published: true, author_id: 5)Book.create(title: "XYZ Book", description: "Astonishing", publisher: "BooksForAll", language: "Englisg", price: 8.00 , length_page: 260, published: false, author_id: 6)Book.create(title: "Book", description: "Impressive", publisher: "SuperBooks", language: "Russian", price: 3.00 , length_page: 10, published: true, author_id: 7)
To provide a mapping between HTTP verbs/requests and URLs to controller actions, we need to create routes
for both resources, here is our route code, we need to write in routes.rb
file

All right, almost there! Let’s generate two controllers
and define actions inside these controllers. These actions / methods answer a question, what to do, when a user routes to specific URLs such as … /authors
or …/books
or … /authors/id
or …/books/id
. Please run the commands below and write the code.
rails generate controller Authors
rails generate controller Books


Here we go! Run a server by rails server
command on your machine and navigate to the following links: http://localhost:3000/authors or http://localhost:3000/books or : http://localhost:3000/authors/1 or http://localhost:3000/books/1 If everything has been done correctly, the browser displays something like these snapshots below / for authors
and books
resources.
By default, Ruby on Rails provides a way to render JSON, so the browser renders exactly what we have in both backend tables; all the records / fields are shown. If you want to control what data to render in the browser, then we need to use serializers.
The book
resource

The author
resource

The book
individual resource at index 1

The author
individual resource at index 1

Conclusion
In this short tutorial, we have learned that any Rails application follows Model-View-Controller (MVC) pattern, each component of this pattern can be created by applying appropriate Rails commands or generators. The Model component communicates with database. The Controller component is responsible for handling incoming HTTP requests and providing a suitable response. In this case, the controller generated JSON response we rendered in the browser. The view layer consists of JSON data that is returned from our controllers. We also implemented two routes in routes.rb file, since the application processes the requests through that file. Then the route file maps the request through whichever controller method is invoked and finally the controller then uses the model to access the requested data from the database, and sends that data back in the response.
My congratulations on building your first Ruby on Rails application!
If you find this information useful, please feel free to follow me! Hope you enjoyed this short guidance on How to create a Ruby on Rails Application, be strong and stay tuned!