Getting Started

Ruby on Rails Fundamentals
Course 1 ยท Chapter 1 ยท Getting Started

๐Ÿš‚ Getting Started with Rails

Ruby on Rails is a full-featured web framework built directly on top of everything covered in the two Ruby courses โ€” blocks, modules, metaprogramming, and Active Record's heavy use of the block/mixin mechanics from Ruby Course 1 all show up again here. Rails' defining philosophy is convention over configuration: an enormous amount of what Express required you to wire up by hand โ€” routing, folder structure, the controller/model split โ€” Rails generates and assumes automatically, as long as you follow its naming conventions.

๐Ÿ“ฆ Installing Rails and Creating a New App

$ gem install rails   # Rails is itself just a gem -- Ruby Course 2, Chapter 7
$ rails new blog_app
$ cd blog_app
$ rails server         # starts the dev server at http://localhost:3000

rails new blog_app does something Express never does on its own: it generates a complete, working application skeleton โ€” folders, a database configuration, a Gemfile with sensible defaults, and a functioning (if empty) app you can immediately run. Compare this to Express's npm init plus manually installing and wiring together every piece.

๐Ÿ“ The Generated Directory Structure

blog_app/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ controllers/
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ views/
โ”‚   โ””โ”€โ”€ assets/
โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ routes.rb
โ”‚   โ””โ”€โ”€ database.yml
โ”œโ”€โ”€ db/
โ”‚   โ”œโ”€โ”€ migrate/
โ”‚   โ””โ”€โ”€ schema.rb
โ”œโ”€โ”€ Gemfile
โ””โ”€โ”€ Gemfile.lock

Every one of these folders already exists, already has a defined purpose, and Rails' generators (previewed at the end of this chapter) know exactly where new code belongs. app/controllers, app/models, and app/views aren't a structure you invent โ€” they're the framework's built-in Model-View-Controller layout, present the moment rails new finishes running.

โš–๏ธ Convention Over Configuration

This is Rails' central idea, and it's worth stating plainly: if you follow Rails' naming conventions, an enormous number of decisions get made for you automatically, with no explicit configuration required.

Naming implies wiring

A controller named PostsController automatically lives at app/controllers/posts_controller.rb, automatically renders views from app/views/posts/, and โ€” once Chapter 2 covers resources :posts โ€” automatically gets a full set of RESTful routes, with zero additional configuration.

Database tables, inferred

A model named Post automatically maps to a database table named posts (pluralized, snake_case) โ€” Active Record (Chapter 5) infers this mapping rather than requiring it to be spelled out anywhere.

๐Ÿ—๏ธ MVC: Baked In vs Hand-Assembled

Recall Express Course 1's final chapter, which walked through manually organizing a Controller-Service-Model structure: a hand-written src/controllers/, src/services/, src/models/, and src/routes/, each layer wired together explicitly by you, one require and one router mount at a time. Rails' app/controllers, app/models, and app/views aren't something you build โ€” they exist from the first command.

Project Structure: Express vs Rails

ConcernExpress (hand-assembled)Rails (built in)
Where does this exist?You design and create it yourselfGenerated automatically by rails new
Controllerssrc/controllers/ (your convention)app/controllers/ (Rails' convention)
Models / data accesssrc/models/ (your convention)app/models/ (Rails' convention, Active Record built in)
Business logic layersrc/services/ โ€” Express has no opinion, you invented this layerNo built-in equivalent โ€” Rails traditionally puts logic in models ("fat models"); a services layer is a common addon pattern, not a framework default
Route wiringManual router.use() mounting per resourceconfig/routes.rb, often one resources line per resource
โš  Convention over configuration cuts both ways

Express's explicitness means every piece of wiring is visible somewhere in your own code โ€” nothing happens that you didn't write. Rails' conventions mean a lot happens that you didn't write, inferred purely from a file's name and location. This is faster once you know the conventions, and genuinely confusing before you do โ€” "magic" is the word often used, sometimes as praise, sometimes as a complaint. Expect an adjustment period where things work and you're not entirely sure why, before the conventions click.

๐Ÿญ A First Taste of Generators

$ rails generate controller Welcome index
      create  app/controllers/welcome_controller.rb
      create  app/views/welcome/index.html.erb
      route   get "welcome/index"

rails generate (often shortened to rails g) scaffolds files following Rails' conventions instantly โ€” a controller file, a matching view, and even a route entry, all created and correctly wired together in one command. This is the generator mechanism Chapters 2 through 8 lean on repeatedly for routes, models, and migrations.

๐Ÿ“œ Starting a Project: Express vs Rails

Side by Side

StepExpressRails
Create the projectnpm init, then manually install expressrails new blog_app
Project structureYou design it (express1-8 walked through one approach)Generated automatically, following Rails conventions
Start the dev servernode server.js (or nodemon)rails server
Scaffold a new resourceManually create controller/service/model/route filesrails generate controller/model/scaffold

๐Ÿ’ป Coding Challenges

Challenge 1: Tour the Generated App

Run rails new library_app (or read through the structure shown in this chapter if you don't have Rails installed locally) and write one sentence each describing the purpose of app/, config/, db/, and the Gemfile.

Goal: Build a working mental map of where things live before writing any actual Rails code.

โ†’ Solution

Challenge 2: Map Express's Structure Onto Rails

Using the Express project structure from this chapter's comparison table (src/controllers, src/services, src/models, src/routes), write down which Rails folder each one maps to โ€” and explicitly note which one has no direct Rails equivalent, and why.

Goal: Practice translating a structure you already know into Rails' conventions.

โ†’ Solution

Challenge 3: Generate a Controller

Write the exact command to generate a controller called Products with two actions, index and show. List every file (and route entry) you'd expect Rails to create as a result, based on the pattern shown in this chapter's Welcome example.

Goal: Predict a generator's output before running it, based on the naming conventions covered so far.

โ†’ Solution

๐Ÿ’Ž The Trade This Whole Course Is About

Nearly every chapter from here on is some version of the same trade: Rails gives up some of Express's explicitness in exchange for dramatically less boilerplate, as long as you're building something that fits its conventions. That's a genuinely different bet than Express's philosophy โ€” neither is simply "better," and this course will keep pointing out exactly where that trade shows up, starting with routing in the next chapter.

๐ŸŽฏ What's Next

Next chapter: Routing โ€” config/routes.rb, and how a single resources :posts line generates all seven RESTful routes Express would otherwise need defined one at a time.