Understanding Rails · Season 2: Getting started
The Rails way
Episode 12 · overview · about 2 minutes · Rails 8.1 · by Tommy A. Caruso
How was this episode?
The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.
Not endorsed yet. Are you a Rails developer? What's an endorsement?
Endorse this version To endorse: create an account, confirm your email, then link GitHub.
Convention over configuration, MVC, and why Rails picks defaults for you.
Free to watch. If it helped, support the series.
Transcript
0:00 The Rails way
Episode twelve. The Rails way. Rails makes a lot of choices for you. Here's why, and how to work with them.
0:11 One name
Start with one word: note. From that one name, Rails knows where everything lives.
0:18 Where the name leads
The model is Note, singular. Its table is notes, plural. The controller is NotesController, in notes underscore controller dot rb. And its views live in a folder called notes.
0:31 Convention over configuration
In many frameworks, you'd write configuration to connect each of those. In Rails, the names are the connection. That's convention over configuration.
0:42 The same path every time
The same goes for the shape of every request: route, controller, model, view. You followed that path in season one, and every Rails app takes it.
0:52 Omakase
Rails also picks your tools. Its creator calls it omakase: a set menu, where the chef chooses for you.
1:01 The menu
SQLite for data. The Solid trio for jobs, caching, and live updates. Propshaft and import maps for assets. Hotwire for fast pages. Minitest for tests. And Kamal to ship.
1:15 Swapping a default
You can swap any of them. But each swap is a choice you now own, and fewer guides will match your app.
1:22 Why it pays
Follow the conventions, and any Rails developer can open your app and find their way around in minutes.
Next: bin slash rails, the command you'll use every day.
Check your understanding
-
You make a model named BookClub. Which table does Rails expect it to use?
- BookClub
- book_club
- book_clubs
- bookclubs
Show answer
book_clubs. The model name is singular and CamelCase. Rails pluralizes it and uses snake_case for the table, so BookClub maps to book_clubs. Source: Naming Conventions, Active Record Basics (Rails Guides 8.1).
-
Which controller name follows the Rails convention for the Note model?
- NotesController
- NoteController
- NotesControllers
Show answer
NotesController. Controllers are named for the plural resource, while models stay singular. That lets resources :notes find NotesController with no extra options. Source: Controller Naming Convention, Action Controller Overview (Rails Guides 8.1).
-
NotesController#index sets @notes and never calls render. What does Rails show?
- A blank page
- An error, because there is no render call
- The template app/views/notes/index.html.erb
- The JSON for @notes
Show answer
The template app/views/notes/index.html.erb. With no explicit render, Rails looks for the template named after the action, in the controller's view folder. Source: Rendering by Default, Layouts and Rendering in Rails (Rails Guides 8.1).
-
What does convention over configuration mean in Rails?
- You must list every file's location in a config file
- Rails has defaults for how things are named and placed, so you don't configure them yourself
- Configuration files are not allowed in Rails apps
- Each developer picks their own conventions
Show answer
Rails has defaults for how things are named and placed, so you don't configure them yourself. Rails defaults to a set of conventions instead of asking you to define them through endless configuration files. Source: Rails Philosophy, Getting Started with Rails (Rails Guides 8.1).