In a Rails ApplicationController, why do shared methods like current_market belong under private, and what does helper_method add?
answer
- public methods become actions
- action_methods stops at abstract Base
- ApplicationController is not abstract
- helper_method delegates via controller.send
- memoize with ||= per request
basics
~10 sEvery public method of a Rails controller, including those inherited from ApplicationController, counts as an action, so shared code goes under private; helper_method :current_market then lets views call that private method through the controller.
solid answer
~40 s`AbstractController::Base.action_methods` is the set of a controller's public instance methods minus those defined by `ActionController::Base` and its ancestors, which are marked `abstract!`. `ApplicationController` is not abstract, so a public `current_market` there becomes an action of **every** controller: a route that names it can reach it, it shows up in `action_methods`, and every `before_action` runs in front of it. Under `private` it is just a method. Views cannot call controller methods directly, so `helper_method :current_market` defines a view helper that delegates with `controller.send(:current_market, ...)` — `send`, so it works on private methods. Memoize with `@current_market ||=` so the controller and the views share one lookup per request.
code
ruby · 11 linesclass ApplicationController < ActionController::Base
allow_browser versions: :modern
stale_when_importmap_changes
helper_method :current_market
private
def current_market
@current_market ||= Market.find_by(slug: request.subdomain)
end
endgo deeper
Remember the rule: actions are public methods, everything else in a controller goes under private, and helper_method makes a controller method usable in views.
Explain how action_methods subtracts the abstract ActionController::Base's methods, why ApplicationController's public methods become actions everywhere, and how helper_method delegates with send.
Review base controllers for accidental public methods, memoize per-request lookups correctly, and keep the list of helper_method declarations small enough that views stay simple.
Judge how much cross-cutting behaviour belongs in ApplicationController versus concerns or intermediate base controllers, balancing discoverability against every controller inheriting everything.
## What the generated ApplicationController holds `rails new` writes an `ApplicationController < ActionController::Base` that every other controller inherits from. In Rails 8.1 it contains `allow_browser versions: :modern` and, in an import-map app, `stale_when_importmap_changes`. It is the natural home for cross-cutting behaviour: callbacks, a `current_market` lookup, rescue handlers. That is exactly where the rule about public methods matters most. ## Which methods are actions Rails does not keep a list of actions you declare; it derives one. - `AbstractController::Base.internal_methods` collects the public methods of the nearest **abstract** ancestor, `ActionController::Base`, which calls `abstract!`. - `action_methods` is every public instance method of the controller, inherited ones included, **minus** those internal methods, cached as a set of strings. - `ApplicationController` does not call `abstract!`, so its public methods are not internal. They are actions of every subclass. - Protected and private methods are never actions. Implicit render widens this: a template whose name matches an action name also counts as an action, even with no method. ## Why a public helper is a problem | Concern | Public `current_market` in ApplicationController | |---|---| | Routing | Reachable by any route that names it, including a deprecated dynamic `:action` segment | | Implicit render | A request to it renders a template or falls back to an error or 204 | | Callbacks | `only:` and `except:` lists can match it, and every `before_action` runs before it | | Introspection | Appears in `VendorsController.action_methods` and in tools that read it | In a codebase that only uses `resources` routes the method may never be reached, but "safe because no route points at it" is a fragile guarantee. The convention is simple: actions are public, everything else is `private`. ## Spotting the problem in review Accidental actions are easy to miss because nothing fails. A few checks catch them: 1. Every method in a controller that is not meant to answer a route sits below a `private` line; the scaffold's own layout, with `set_*` loaders and `*_params` methods under `private`, is the model. 2. Concerns included into controllers follow the same rule: a public method in a concern becomes an action of every controller that includes it. 3. In a console, `SomeController.action_methods` lists exactly what Rails considers routable for that controller; anything surprising there is a candidate for `private`. 4. Intermediate base controllers count too: a public method in an `Admin::BaseController` is an action of every admin controller beneath it, which multiplies the exposure. None of these checks needs tooling; they need the habit of reading a controller's public surface as its routable surface. ## Exposing controller methods to views Templates run in a separate view context object, not in the controller, so they cannot call controller methods. `helper_method` bridges that gap: 1. `helper_method :current_market` records the name in `_helper_methods`. 2. It defines a method with the same name on the controller's helper module. 3. That method calls `controller.send(:current_market, ...)`, forwarding all arguments. 4. Because it uses `send`, the controller method can and should stay private. The opposite direction exists too: inside a controller, `helpers` returns a view context that exposes the view helpers, for example to format a value in a flash message. ## Making the helper cheap - Memoize: `@current_market ||= Market.find_by(slug: request.subdomain)`. The instance variable lives for one request on one controller instance, so the controller, its callbacks and every template share one query. - Do not memoize with `||=` when `nil` or `false` is a meaningful result you want to keep; the lookup will rerun every time. - Keep the method small. A long list of `helper_method` declarations in `ApplicationController` is often a sign that views know too much about the request. ## Common mistakes - Leaving `current_market` public "because views need it" — views need `helper_method`, not public visibility. - Using `protected` and believing it changes visibility to views; it only removes the method from actions. - Defining the same logic twice, once in a helper module and once in the controller, instead of exposing the controller's version.
- Why are the public methods of ActionController::Base itself never Rails actions?`ActionController::Base` calls `abstract!`. `internal_methods` walks up from the controller to the first abstract class and treats that class's public methods as internal, and `action_methods` subtracts them. Methods such as `render` or `params` are therefore never routable, while anything public added below that abstract class is.
- How does a Rails controller call a view helper such as number_to_currency?Through `helpers`, which returns a view context carrying the view helper modules, for example `helpers.number_to_currency(price)` when building a flash message. It is the reverse of `helper_method`, which lets views call controller methods.
- Only methods mapped in config/routes.rb count as actions, so public helpers are harmless.
- Methods defined in ApplicationController are never actions because it is the base class.
- helper_method requires the controller method to be public so the view can call it.
- Marking a method protected makes it callable from views but hides it from routes.
- Views run inside the controller instance, so they can call any controller method.