skip to content

In the Rails 8.1 console, what do reload!, app and helper give you, and what catches people out with each?

level: middleimportance: should knowfreq 42%

answer

  1. IRB helper methods Rails registers
  2. old objects keep the old class
  3. reload! is a no-op without reloading
  4. integration session, host www.example.com
  5. ApplicationController.helpers proxy

basics

~20 s

reload! reloads changed application code, but objects already in variables keep their old class; app is an integration session that can call route helpers and send requests without a server; helper is ApplicationController's view-helper proxy for trying formatting helpers.

solid answer

~40 s

Rails registers these as IRB helper methods when the console starts. `reload!` calls the application reloader: in development it unloads and reloads code that changed on disk, but a record you assigned earlier still points at the old class, so re-fetch it; in production, where reloading is off, it reloads nothing. `app` returns a memoized `ActionDispatch::Integration::Session`, the same object integration tests use: `app.rentals_path` builds URLs and `app.get "/rentals"` runs a request through the full middleware stack and router with no server, using `www.example.com` as the default host. `app(true)` gives a fresh session. `helper` is `ApplicationController.helpers`, a view context with Rails' helpers and the app's `app/helpers` modules, so `helper.number_to_currency(42)` works. There is also `controller` for an `ApplicationController` instance.

code

ruby · 12 lines
ruby
r = Rental.first
# ... add Rental#late_fee to app/models/rental.rb ...
reload!               # => prints "Reloading..."
r.late_fee            # NoMethodError: r is an instance of the old class
r = Rental.first
r.late_fee            # works

app.rentals_path                                   # => "/rentals"
app.get "/rentals", headers: { "Host" => "localhost" }
app.response.status                                # => 200

helper.number_to_currency(129.5)                   # => "$129.50"

go deeper

for a junior

Know that reload! picks up edited code and that you must re-fetch objects afterwards. Know app and helper exist for routes and view helpers.

for a middle

Explain why stale objects survive reload!, what an integration session is, and why helper has no request context. Mention the default www.example.com host.

for a senior

Use app to reproduce a request path end to end from a console, and know reload! is meaningless in production, so a deploy means a new console.

for a principal

Decide which console conveniences a team standardizes, for example shared IRB helper methods, and keep them out of production consoles when they could mutate data.

## Where the helpers come from `bin/rails console` starts **IRB** with the application loaded, then registers a few Rails-specific **helper methods** with IRB's own `IRB::HelperMethod.register` API. In Rails 8.1 they are `reload!`, `app`, `helper`, `controller` and the deprecated `new_session`. This registration replaced the old way of extending the console by reopening `Rails::ConsoleMethods`, which Rails 8.0 removed (8.1 then deleted the `rails/console/methods` file that 8.0 had deprecated). | Helper | Returns | Typical use | |---|---|---| | `reload!` | runs the app reloader | pick up code you just edited | | `app` | `ActionDispatch::Integration::Session` (memoized) | route helpers, fake requests | | `app(true)` | a fresh integration session | start over without old cookies | | `helper` | `ApplicationController.helpers` | try view helpers | | `controller` | `ApplicationController.new` (memoized) | inspect controller-level methods | ## reload! `reload!` prints `Reloading...` and calls `Rails.application.reloader.reload!`. Two things trip people up: 1. **Stale objects.** Reloading removes the old constants and lets the autoloader define them again. A variable that already holds a record, say `r = Rental.first`, still holds an instance of the **old** `Rental` class object, so a method you just added is missing on it. Run `r = Rental.first` again after `reload!`. 2. **It only works where reloading is enabled.** In development the reloader checks whether watched files changed and reloads if they did. In production, reloading is disabled, the reloader's check always answers no, and `reload!` changes nothing. A new deploy needs a new console process. ## app `app` is an **integration session**, the same class Rails' integration tests drive. It is created once per console and memoized; `app(true)` replaces it. - It includes the application's **URL helpers**, so `app.rental_path(42)` or `app.root_url` answer exactly what views would generate. - It can **send requests**: `app.get "/rentals"`, `app.post ...`, then inspect `app.response.status`, `app.response.body`, `app.cookies` or `app.session`. The request goes through the real middleware stack, router and controller in-process; no server is started and no port is opened. - Its default host is **`www.example.com`**. Development's host authorization admits `.localhost` and any IP address, not that name, so pass `headers: { "Host" => "localhost" }` or call `app.host! "localhost"` first, or the request may be blocked. ## helper and controller `helper` returns `ApplicationController.helpers`, a view context built for the console. Because `include_all_helpers` defaults to true, it carries Rails' own helpers plus every module in `app/helpers`: - `helper.number_to_currency(129.5)` to check formatting; - `helper.time_ago_in_words(3.days.ago)`; - `helper.season_badge(rental)` for your own helper method. It has no request, so helpers that read `request`, `params` or the current user will fail or behave differently than in a real view. `controller` gives a bare `ApplicationController` instance for the same kind of poking. ## Mistakes worth avoiding - Expecting `reload!` to update objects you already loaded. - Running `reload!` in a production console after a deploy and trusting the result. - Thinking `app.get` hits a running server, or that it needs one. - Adding console helpers by reopening `Rails::ConsoleMethods` on Rails 8; register an IRB helper method or define a method in an `.irbrc` instead. - Treating `helper` as a full view: anything that needs request context belongs in an integration request through `app`. ## Using app beyond a single GET The integration session keeps state between calls, which is what makes it useful for reproducing a flow from the console: - `app.post "/session", params: {...}` signs in, and the session cookie it gets back is sent on the next request; - `app.follow_redirect!` follows the last redirect, as a browser would; - `app.https!` switches the session to HTTPS when the app forces SSL; - `app.response.headers` and `app.response.body` show exactly what the controller produced. Because state accumulates, `app(true)` is the clean way to start over without reopening the console. None of this involves a browser, JavaScript or a network port; it is the Rack stack called in-process.

  • How do you add your own helper method to the Rails 8.1 console?
    Rails 8 removed extending the console through `Rails::ConsoleMethods`. Rails itself registers its helpers with IRB's `IRB::HelperMethod.register`, passing a subclass of `IRB::HelperMethod::Base` with an `execute` method, and an app can do the same in a `console` block of its application config. For a personal shortcut, a method in your `.irbrc` is enough.
  • Why does app.get '/rentals' sometimes come back blocked in a development console?
    The integration session's default host is `www.example.com`. In development, host authorization admits `.localhost`, `.test` and any IP address, but not that name, so the request is blocked. Pass a `Host` header such as `localhost` or call `app.host! "localhost"` before the request.

  • reload! updates records already stored in console variables
  • app.get starts a Puma server on port 3000 to answer
  • helper only exposes Rails' built-in helpers, not app/helpers
  • reload! in a production console picks up freshly deployed code
  • Rails 8.1 console helpers are added by reopening Rails::ConsoleMethods

Assumes Rails 8.1, where console helpers are IRB helper methods; Rails::ConsoleMethods extension was removed in 8.0.