Rails console (IRB)
This course covers the Rails console, started with bin/rails console or bin/rails c. The console is IRB, Ruby's interactive shell, with every class of the app already loaded. No server, browser or HTTP request is involved. The lessons build a model instance and call its methods, then use the app and helper objects to visit routes and call view helpers. They also cover reload! for edited code, inspect mode, the underscore variable, and sandbox mode, which rolls back every write on exit.
What you'll learn
- ✓Starting and leaving the console with bin/rails console, bin/rails c, exit and Ctrl+D
- ✓Running plain Ruby and the app's classes at the prompt without require
- ✓Building model instances and calling their methods without a browser
- ✓Visiting routes with app and calling view helpers with helper
- ✓Reloading code with reload!, reading inspect mode output and reusing results with _
- ✓Experimenting with --sandbox, where writes roll back on exit
Starting the Rails console
So far this arc has been about bin/rails server, Puma and a browser sending HTTP requests. There's another way to run Rails code that skips the browser, the server and HTTP: the Rails console.
Read the full lesson free →Curriculum
7 lessonsStarting the Rails console
Starts the console with bin/rails console, an IRB session with the app loaded, and leaves it with exit.
Plain Ruby, with your app already loaded
Plain Ruby works as usual in the console, and every class the app defines is loaded without require.
Trying models without a browser
Builds an Article instance and calls published? at the prompt, with no controller, view or route involved.
Two special objects: app and helper
Uses app to send paths through routing and helper to call view helpers like link_to and pluralize.
reload! and inspect mode
Explains reload! for picking up edited code, inspect mode output with quoted strings, and the _ variable.
Sandbox mode: consequence-free experiments
Runs bin/rails console --sandbox, where every write sits in one transaction that rolls back on exit.
Putting it together: a full console session
Walks through one sandboxed session that uses every lesson: models, app, helper, reload! and inspect mode.