Welcome to your journey of building a ToDo app with Ruby on Rails! In this section, you will learn how to set up a new Rails application, which is the first and crucial step in making your app functional. This foundational knowledge will serve as a stepping stone for all future development tasks.
By the end of this lesson, you should be able to set up a new Rails application with a clear understanding of its project structure.
Before getting into Ruby on Rails, let's review what REST is, as it is crucial for the remainder of the course. REST (Representational State Transfer) is a way to design web APIs so that different programs can talk to each other over the internet. REST uses simple HTTP methods to perform tasks. Here are the common methods and what they do:
- GET: Retrieve data from the server. For example, getting a list of to-do items.
- POST: Send data to the server to create a new resource. For instance, adding a new to-do item.
- PUT: Update an existing resource entirely on the server. For example, updating all details of a to-do item.
- PATCH: Update part of an existing resource on the server. For instance, marking a to-do item as completed.
- DELETE: Remove a resource from the server. For example, deleting a to-do item.
In REST, everything is treated as a resource and is accessed using a specific URL. This makes it easy to understand and use.
By following REST principles, your ToDo app will have a clear and predictable API structure, making it easier to develop, maintain, and use by other applications.
Now that we have refreshed our knowledge on REST, we'll move on to the topic of today: Ruby on Rails. Ruby on Rails, or Rails for short, is a web application framework written in Ruby follows the model-view-controller (MVC) architecture, which helps developers build database-backed web applications efficiently. It consists of many different components working together, such as different gems, tools, and more. Rails emphasizes the use of well-known software engineering patterns and makes assumptions about what every developer needs to get started. This means you can focus on writing your application code rather than reinventing the wheel.
Let's start with the initial setup of creating a new Rails application. After installing Rails and Bundler with gem install rails bundler
, run the following command to generate a new Rails project:
Bash1rails new todo_app
This command will create a new Rails application named todo_app
, ready to be executed.
Once the project has been generated, a directory structure will be created on your disk. Although it includes numerous directories and files, we will focus only on the most relevant ones, as shown below. Grasping the structure and setup of a Rails application is essential for efficient development. Let's examine what the generated project contains:
1todo_app/ 2├── app/ 3│ ├── controllers/ 4│ │ └── application_controller.rb 5│ ├── models/ 6│ │ └── application_record.rb 7├── config/ 8│ ├── routes.rb 9│ ├── application.rb 10│ └── database.yml 11├── db/ 12│ ├── migrate/ 13│ ├── schema.rb 14└── Gemfile
Each part serves a unique role:
-
The
app/
directory contains the main logic of your application, including controllers, services, and models. These topics will be covered later, but it's good to know where they reside. -
The
config/
directory holds all the configuration files of your application. -
The
db/
directory handles database migrations and schema, and will only be created after installing the database. -
The
Gemfile
lists the dependencies required for your Rails project. -
The
config/routes.rb
file is of extra importance, as it defines the HTTP routes for your Rails application.
In this lesson, we set up an API-only ToDo app using Rails. We introduced Ruby, highlighting its simplicity and productivity. We learned about gems and managing them with Bundler and a Gemfile
. Rails, following the MVC pattern, offers rapid development, built-in testing, and a rich gem ecosystem. We generated a Rails project and explored its structure, including key directories like app/
, config/
, and db/
. We also covered config/application.rb
for app configuration and config/routes.rb
for defining todos
routes.
With this knowledge, you will be unstoppable when writing Rails servers. Are you ready to try it out? Let's jump into the practice exercises!