Framework cheatsheet
Ruby on Rails 8 Cheatsheet
Create apps, generate code, manage the database and run tests with the Rails 8 command line.
Getting started
- Install Rails — Install the Rails gem. Needs Ruby installed.
- Check the Rails version — Print the installed Rails version.
- Create an app — Generate a new Rails application in a folder named myapp.
- Create an app that uses PostgreSQL — Choose a database other than the default SQLite.
- Create an API-only app — Skip the view layer and middleware that browser apps need.
- Install the gems — Install the gems listed in the Gemfile.
- Start the development server — Run the app at http://localhost:3000.
- Start the server on another port — Use a different port when 3000 is taken.
- Open the Rails console — Start an IRB session with the app loaded. Handy for trying out models.
- List the routes — Show every route, with the controller action it maps to.
- Show app and environment details — Print the Ruby, Rails and database versions and the environment.
Generators
- Generate a model — Create a model, its migration and a test.
- Generate a controller — Create a controller with index and show actions and their views.
- Generate a full CRUD scaffold — Create model, controller, views, routes and tests in one go.
- Generate a migration — Create a migration that adds a column. Rails reads the intent from the name.
- Generate authentication — Add the built-in authentication system introduced in Rails 8: sessions, users and password reset.
- Generate a background job — Create an Active Job class.
- Undo a generator — Remove the files a generator created.
Database
- Create the databases — Create the development and test databases.
- Run migrations — Apply pending migrations and update db/schema.rb.
- Check migration status — Show which migrations have been run.
- Roll back the last migration — Undo the most recent migration.
- Load seed data — Load the data in db/seeds.rb into the database.
- Prepare the database — Create the database if it is missing, then migrate. Safe to run repeatedly.
- Drop and recreate the database — Drop, recreate, load the schema and seed.
- Open the database shell — Open psql, mysql or sqlite3, depending on your configuration.
Testing
- Run all tests — Run every test except system tests.
- Run one test file — Run the tests in a single file.
- Run one test — Run the test that starts on line 12 of a file.
- Run system tests — Run browser-driven tests.
- Show code statistics — Count lines of code and tests.
Maintenance and tools
- Edit encrypted credentials — Open config/credentials.yml.enc in your editor. Set EDITOR first, for example EDITOR="code --wait".
- Run code in the app — Run a Ruby snippet with the app loaded.
- List TODO comments — Find TODO, FIXME and OPTIMIZE annotations in the code.
- List the middleware stack — Show the middleware requests pass through.
- Check autoloading — Verify that file names match the constants they define.
- Clear temporary files — Clear cache, sockets and sessions in tmp.
- Precompile assets — Build the asset files for production.
- Update config for a new Rails version — After upgrading the gem, update configuration files, reviewing each change.
Deployment with Kamal
- Set up servers and deploy — Install Docker on the servers if needed, set up accessories and deploy for the first time.
- Deploy a new version — Build and push the image, then roll it out with no downtime.
- Follow the app logs — Stream logs from the app containers.
- Open a Rails console on the server — Run a console in the production container. This is an alias that the Rails 8 config/deploy.yml defines.
- Roll back a release — Switch back to an earlier version, using a version shown by kamal app containers.
Related cheatsheets
- Angular 22 cheatsheet — Angular CLI commands and standalone component, signals, template, routing, HTTP and forms snippets.
- Astro cheatsheet — Build fast content sites with Astro: components, layouts, routing, content collections, islands, endpoints and deployment.
- Django 6 cheatsheet — Create projects, run migrations, test and deploy Django apps, plus the ORM queries you use every day.
- .NET and ASP.NET Core 10 cheatsheet — The dotnet CLI for creating, building, testing and publishing projects, plus common ASP.NET Core and EF Core code.
- Express 5 cheatsheet — Build HTTP servers and APIs with Express 5 on Node.js.
- FastAPI 0.115 cheatsheet — Install, run, test and deploy FastAPI apps from the command line.