Chapter 3: Creating Your First Rails Application

Chapter 3: Creating Your First Rails Application

AI Reading

Quick summary of this article

Creating a new Rails application is done with a single command, which generates a complete project folder with all necessary files organized by convention. This chapter walks through creating a project, starting the development server, and understanding the key folders and files in the default structure.

  • Run rails new myapp in the terminal to create a new project folder named "myapp" with the full application, required gems, and a default database.
  • Start the built-in development server by running rails server inside the project folder, then visit http://localhost:3000 in your browser to see the Rails welcome page.
  • The most important folders are app/controllers (handles requests), app/models (database interaction), app/views (HTML templates), and config/routes.rb (defines URLs).
  • Database migrations are stored in db/migrate/, and the Gemfile lists all Ruby gems used by the application.
  • Seeing the Rails welcome page at localhost:3000 confirms that your setup is correct and ready for the next chapter on routing.

Chapter 3: Creating Your First Rails Application

Creating a new Rails project is a single command. Rails will generate a complete project folder with all the files and folders you need, organised by convention. In this chapter you will create your first application, start the development server, and explore the project structure.

🚧 Creating a New Project

Open your terminal and run:

rails new myapp

Rails creates a folder called myapp containing the whole application, installs the required gems, and prepares the default database.

🚀 Starting the Server

Move into the project folder and start the built-in development server:

cd myapp
rails server

Now open your browser and visit http://localhost:3000. You should see the Rails welcome page. Congratulations, your first Rails application is running!

📁 Understanding the Project Structure

Inside the project folder you will see many files. Do not be overwhelmed; you only need a few of them every day:

  • app/controllers/ – controller files that handle requests.
  • app/models/ – model files that talk to the database.
  • app/views/ – HTML templates (ERB files) for each action.
  • config/routes.rb – the file where you define your URLs.
  • db/migrate/ – database migrations that create and change tables.
  • Gemfile – lists the Ruby gems your application uses.
# Key folders inside your Rails app
myapp/
  app/
    controllers/
    models/
    views/
  config/
    routes.rb
  db/
    migrate/
  Gemfile

🏠 Practice Exercises

# 1. Create a new Rails project
rails new myapp

# 2. Move into it and start the server
cd myapp
rails server

# 3. Open http://localhost:3000 in your browser

If you see the Rails welcome page, everything is set up correctly. In the next chapter you will learn how routing connects URLs to controllers.

Leave a Reply

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