Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

Your First Rails App

Install Rails, run rails new, start the server and tour the folders of a new app. Includes a runnable Ruby stand-in that scaffolds a project tree so you see what the generator does.

The fastest way to understand Rails is to create an app and look around. In this lesson you will install Rails, generate a project with rails new, start the development server, and learn what every important folder is for. You will also generate your first page with a generator and see where its files land.

All commands below are read-only on this site because the runner has no Rails and no network. Run them in your own terminal. After the commands we build a runnable stand-in that creates a fake project tree, so you can see what a generator actually does: it writes files with predictable names.

Installing Rails

You need Ruby (see Ruby versions), a database library, and Node.js is optional in current Rails because JavaScript can come from import maps. Check your Ruby, then install the gem:

Terminal
ruby -v                 # Rails 8 needs a recent Ruby 3.x or 4.x
gem install rails
rails -v                # prints the installed Rails version

Rails depends on dozens of gems. Bundler installs and locks them for each project, so two apps can use different Rails versions side by side.

Generating an app

Terminal
rails new blog
cd blog
bin/rails server

rails new blog creates a folder called blog, writes about seventy files, runs bundle install and sets up the database. SQLite is the default database, which needs no server, so the app runs right away. bin/rails server starts the web server (Puma) on port 3000. Open http://localhost:3000 and you see the Rails welcome page.

Useful options:

Terminal
rails new blog --database=postgresql   # use PostgreSQL instead of SQLite
rails new blog --api                   # slim JSON-only app, no views
rails new blog --css=tailwind          # add a CSS framework
rails new blog --skip-test             # skip the default test files

A tour of the folders

Open the new project. These are the places you will spend your time:

Where your code lives

app/controllers One class per resource.
app/models Active Record classes.
app/views ERB templates.
1 / 3

A few details worth knowing:

  • app/ is autoloaded. Any class in app/models, app/controllers and friends is found by name, with no require lines. See Rails conventions for how.
  • app/assets, app/javascript and app/views/layouts hold the page frame and its CSS and JS (views chapter).
  • config/routes.rb decides which URL goes to which controller (routing).
  • db/migrate stores database changes as Ruby files (migrations).
  • log/ and tmp/ are created while the app runs and are not committed to Git.

Your first page with a generator

A generator writes boilerplate for you. Ask for a controller with an action:

Terminal
bin/rails generate controller Welcome index

Rails prints the files it created, roughly:

TEXT
create  app/controllers/welcome_controller.rb
 route  get "welcome/index"
create  app/views/welcome/index.html.erb
create  test/controllers/welcome_controller_test.rb
create  app/helpers/welcome_helper.rb

It also adds a route to config/routes.rb. Edit the generated view to say anything you like, set the home page, and reload:

Ruby
# config/routes.rb  (read-only)
Rails.application.routes.draw do
  root "welcome#index"
end

"welcome#index" means the index action of WelcomeController. The controller itself is almost empty, because an action with no code renders the view with the same name:

Ruby
# app/controllers/welcome_controller.rb  (read-only)
class WelcomeController < ApplicationController
  def index
  end
end

Edit app/views/welcome/index.html.erb and refresh the browser; in development mode changes appear immediately, with no restart.

Stand-in: what a generator really does

A generator is a function that maps a name to several file paths and fills in templates. This stand-in builds the file list for a controller, then prints a tree. It only manipulates strings, so it runs anywhere.

Ruby Stand-in: a controller generator that lists its files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The real generators are built on Thor and ERB templates, but the principle matches: name in, predictable files out. That is why a new teammate can open any Rails project and know where everything is.

Exploring with the console and routes

Two commands you will use daily:

Terminal
bin/rails console      # an IRB session with your whole app loaded
bin/rails routes       # prints every route and its controller action

Inside the console you can call model methods directly (Article.count) and test ideas without a browser. rails routes is the quickest way to answer "which URL goes where?".

A tiny stand-in for the idea of a route table: a list of rows you can query.

Ruby Stand-in: printing a route table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Do not edit files in tmp/, log/ or node_modules/, and do not commit config/master.key. Rails ignores them in .gitignore for good reasons; the key decrypts your credentials (configuration).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command creates a new Rails application called shop?

    rails new <name> generates the whole project, runs bundle install and prepares the database.
  2. 2. Where does Rails expect your Active Record models?

    Everything under app/ is autoloaded; models go in app/models, controllers in app/controllers.
  3. 3. What does root "welcome#index" do?

    root defines the route for /, and "welcome#index" means controller#action.
  4. 4. Which command prints all defined routes?

    bin/rails routes lists each route with its verb, path and controller action.

Summary

  • Install with gem install rails, then rails new app_name generates a working project.
  • bin/rails server runs it on port 3000; bin/rails console gives you an interactive session.
  • Code lives in app/ (models, views, controllers), URLs in config/routes.rb, database changes in db/.
  • Generators create files with predictable names; bin/rails routes shows the URL map.
  • In development, code changes show up on refresh without a restart.

Next: The Rails Request Cycle