Getting Started with Ruby on Rails
Ruby on Rails is a powerful web framework that lets you build full-stack applications quickly. It follows the convention over configuration principle, which means Rails makes intelligent assumptions about how your application should be structured. This tutorial walks you through installing Rails, creating your first app, and understanding the basics.
What is Ruby on Rails?
Rails is a server-side web application framework written in Ruby. It was created in 2004 and has since become one of the most popular frameworks for building web applications. Companies like GitHub, Shopify, and Airbnb all use Rails in production.
Rails emphasises two key philosophies:
- Convention over Configuration: Rails provides sensible defaults and conventions that reduce the amount of code you need to write. Instead of configuring every aspect of your application, you follow Rails conventions and let the framework handle the rest.
- Don’t Repeat Yourself (DRY): Rails encourages code reuse and modularity. Write something once, and you can use it everywhere.
Installing Rails
Before installing Rails, make sure you have Ruby installed on your system. You can check by running:
ruby --version
If Ruby is installed, you’ll see output like ruby 3.4.x. Now install Rails using the gem command:
gem install rails
This installs the latest stable version of Rails. To verify the installation:
rails --version
You should see something like Rails 8.0.x.
Creating Your First Rails Application
Now let’s create a new Rails application. Open your terminal and run:
rails new my_first_app
This command creates a new Rails project called my_first_app in a directory of the same name. Rails will automatically:
- Create the directory structure
- Install necessary dependencies
- Set up the database configuration
- Generate essential files
The creation process takes a minute or two. Once complete, navigate into your new application:
cd my_first_app
Understanding the Directory Structure
A newly created Rails application has a well-organised directory structure. Here’s what each folder does:
| Directory | Purpose |
|---|---|
app/ | Contains your application code (controllers, models, views) |
config/ | Configuration files for your application |
db/ | Database migrations and schema files |
public/ | Static files (images, CSS, JavaScript) |
test/ | Test files for your application |
config/routes.rb | Defines your application’s URLs |
The most important directories are inside app/:
app/controllers/— Handles HTTP requests and responsesapp/models/— Represents your data and business logicapp/views/— Templates that get rendered to usersapp/assets/— Stylesheets and JavaScript files
Creating Your First Route and Controller
Let’s create a simple page to see Rails in action. First, generate a controller:
rails generate controller Home index
This creates a HomeController with an index action. It also creates a view file at app/views/home/index.html.erb.
Now open config/routes.rb and add a root route:
Rails.application.routes.draw do
root "home#index"
end
This tells Rails to route the homepage URL (/) to the index action of the HomeController.
Starting the Rails Server
Start the development server:
rails server
Or the shorter version:
rails s
You should see output indicating the server is running, typically on http://localhost:3000. Open your web browser and navigate to that URL. You should see the default Rails welcome page or the content from your HomeController#index action.
Congratulations! You’ve created and run your first Rails application.
Next Steps
Now that you have a working Rails application, continue with the next tutorial in this series to learn about the MVC pattern in Rails. Understanding MVC is fundamental to building Rails applications effectively.
The next tutorial covers how Rails organises code using Models, Views, and Controllers, and why this separation of concerns makes your applications maintainable.