Create Your First Rails App

You have Ruby and Rails installed. Now you create your first Rails application. This topic walks you through generating the app, understanding what gets created, and seeing it run in your browser.

The One Command That Does Everything

Open your terminal. Navigate to a folder where you want to keep your projects. Then run:

rails new myblog

Rails does a lot of work in seconds:

Terminal Output Flow

rails new myblog
      |
      +-- Creates folder: myblog/
      |
      +-- Generates ~40 files and folders
      |
      +-- Runs: bundle install (downloads all gems)
      |
      +-- Runs: yarn install (downloads JS packages)
      |
      Done! App is ready.

The name myblog becomes your app's folder name. You can use any name that has no spaces. Use underscores if needed, like my_blog.

Move Into Your App Folder

cd myblog

All Rails commands from this point on run from inside this folder.

Start the Development Server

rails server

You can also use the shorthand:

rails s

Your terminal shows:

=> Booting Puma
=> Rails 7.1.0 application starting in development
=> Run `bin/rails server --help` for more startup options
* Listening on http://127.0.0.1:3000

Open your browser and go to http://localhost:3000. You see the Rails welcome page — a sign that your app is running correctly.

Your First Page: Hello World

The default Rails welcome page is just a placeholder. Build your own page in four steps.

Step 1: Generate a Controller

In a new terminal window (keep the server running in the other), run:

rails generate controller Pages home

Rails creates several files:

Generated Files:

app/controllers/pages_controller.rb   ← handles requests
app/views/pages/home.html.erb         ← the HTML page
config/routes.rb                      ← updated with new URL
test/controllers/pages_controller_test.rb

Step 2: Add Content to the View

Open app/views/pages/home.html.erb in your editor. Replace its contents with:

<h1>Hello, World!</h1>
<p>My first Rails page is live.</p>

Step 3: Set the Root Route

Open config/routes.rb. Add this line inside the Rails.application.routes.draw block:

root "pages#home"

This tells Rails: when someone visits the homepage, show the home action in the pages controller.

Step 4: Visit Your Page

Refresh http://localhost:3000. Your "Hello, World!" message appears.

How a Request Travels Through Your New App

Browser visits http://localhost:3000
          |
          v
config/routes.rb
  root "pages#home"
          |
          v
app/controllers/pages_controller.rb
  def home
    (no special code needed yet)
  end
          |
          v
app/views/pages/home.html.erb
  <h1>Hello, World!</h1>
          |
          v
Browser displays the page

Stop and Restart the Server

To stop the server, press Ctrl + C in the terminal where it is running. To restart it, run rails s again. You rarely need to restart during development — Rails automatically reloads most changes.

Create an App with a Specific Database

SQLite3 works for learning and development. For production apps, PostgreSQL is the standard choice. Create a Rails app with PostgreSQL using:

rails new myapp --database=postgresql

For this course, SQLite3 (the default) is sufficient.

Other Useful Options When Creating Apps

Command FlagWhat It Does
--apiCreates a lightweight API-only app (no views)
--skip-testSkips generating test files
--skip-javascriptSkips JavaScript setup
--database=postgresqlSets up PostgreSQL instead of SQLite3

What You Just Built

You created a working web application in under five minutes. It runs a server, responds to browser requests, follows the MVC pattern, and serves an HTML page. This is the foundation that every feature in this course builds on top of.

Leave a Comment

Your email address will not be published. Required fields are marked *