skip to content

In a Rails app, why can a method from app/helpers/listings_helper.rb be called in every view, and what does include_all_helpers = false change?

level: seniorimportance: should knowfreq 32%

answer

  1. helper :all for direct subclasses
  2. one flat namespace across views
  3. alphabetical include, last one wins
  4. false: matching helper plus parents'
  5. controllers reach them through helpers

basics

~10 s

By default ApplicationController, as a direct ActionController::Base subclass, calls helper :all, so every app/helpers module reaches every view. With config.action_controller.include_all_helpers = false, each controller gets only its matching helper plus inherited ones.

solid answer

~40 s

`ActionController::Base` has `include_all_helpers` defaulting to `true`, and when a controller class inherits directly from it, Rails calls `helper :all`, which loads every `app/helpers/**/*_helper.rb` module, sorted by path, into that controller's helper module. Other controllers inherit it, so `ListingsHelper#price_label` is callable from an orders view too. It is one flat namespace: if `ListingsHelper` and `OrdersHelper` both define `price_label`, the module included later, `OrdersHelper`, silently wins everywhere. With `config.action_controller.include_all_helpers = false`, each controller gets the helper named after it (`ListingsController` gets `ListingsHelper`) plus what its ancestors have, `ApplicationHelper` through `ApplicationController`, and extra modules come in with `helper SomeHelper`. Controllers call helpers through `helpers.number_to_currency(...)`, and `helper_method` exposes controller methods to views. When one model grows many view methods, a presenter object or a component keeps them out of the global namespace.

code

ruby · 21 lines
ruby
# config/application.rb
module Vinyl
  class Application < Rails::Application
    config.load_defaults 8.1
    config.action_controller.include_all_helpers = false
  end
end

# app/controllers/orders_controller.rb
class OrdersController < ApplicationController
  helper ListingsHelper # opt in: order pages also show listing prices

  def create
    order = Current.user.orders.create!(order_params)
    flash[:notice] = "Charged #{helpers.number_to_currency(order.total)}"
    redirect_to order
  end

  private
    def order_params = params.expect(order: [:listing_id])
end

go deeper

for a junior

Know that methods in app/helpers are available in views and that ApplicationHelper is for app-wide ones.

for a middle

Explain helper :all, the include_all_helpers flag, helper and helper_method, and the helpers proxy in controllers.

for a senior

Diagnose a wrong helper winning a name clash, decide whether to turn include_all_helpers off, and move model-heavy helpers into presenters or components.

for a principal

Set the rule for where view logic lives, helpers, presenters or components, so the shared helper namespace stays small as the team grows.

## Where helper methods come from A Rails view runs inside a **view context**, an `ActionView::Base` object extended with the controller's **helper module**. Three things put methods there: 1. Action View's built-in helpers (`link_to`, `number_to_currency`, `tag` and the rest). 2. Modules from **`app/helpers`**, such as `ApplicationHelper` and `ListingsHelper`. 3. Controller methods exposed with **`helper_method`**, such as `authenticated?` in the authentication generator's concern. `rails new` writes `app/helpers/application_helper.rb`, and the controller and scaffold generators write an empty helper per controller (`--no-helper` skips it). ## The default: every helper in every view `ActionController::Helpers` defines the class attribute `include_all_helpers`, **`true` by default**. When a class inherits directly from `ActionController::Base` (in practice `ApplicationController`) and the flag is on, Rails calls **`helper :all`**. That finds every `*_helper.rb` file under the helpers path, sorts the names, constantizes them and includes each module into the controller's helper module. Every other controller inherits that module. The result: a method in `ListingsHelper` is available in `orders/index.html.erb`, in partials shared across controllers, everywhere. The docs describe the older per-controller behaviour as what you get back by turning the flag off. ## The flat-namespace trap All those modules end up in **one** ancestor chain. When two define the same method name, Ruby calls the one in the module included **last**, and `helper :all` includes them in sorted order: ```ruby module ListingsHelper def price_label(item) = number_to_currency(item.price) end module OrdersHelper def price_label(order) = number_to_currency(order.total) end # With include_all_helpers on, every view calls OrdersHelper#price_label. ``` No error and no warning: the listing page silently calls the order version, which then fails with `NoMethodError` or shows the wrong number. Prefixing names (`listing_price_label`) or moving the logic into objects avoids it. ## Turning it off ```ruby # config/application.rb config.action_controller.include_all_helpers = false ``` With the flag off: - every controller still includes the helper **named after it**, found by its class name (`ListingsController` includes `ListingsHelper` when it exists); - helpers are **inherited**, so `ApplicationHelper`, included by `ApplicationController`, reaches every view; - anything else is opted in with `helper OrdersHelper`, `helper :orders` or a `helper do ... end` block. The Rails plugin generator writes this setting into an engine's dummy app, a hint that engines should not assume an app shares all its helpers. | Setting | ListingsController views see | Name clash between two helpers | |---|---|---| | `true` (default) | every module in `app/helpers` | last one in sorted order wins everywhere | | `false` | `ApplicationHelper`, `ListingsHelper`, explicit `helper` calls | only where both are included | ## Helpers from controllers, and when helpers are the wrong tool - In an action, **`helpers`** returns the view context, so `helpers.number_to_currency(total)` works in a flash message; the class-level `helpers` proxy serves code without a request. - **`helper_method :current_cart`** goes the other way, exposing a controller method to views. - Helpers are global functions with implicit access to the view. When a model collects a dozen display methods (`price_label`, `condition_badge`, `seller_line`), a **presenter** object that wraps the record, or a **component** with its own template and tests, keeps them grouped and out of the shared namespace. Small formatting helpers stay helpers. ## Mistakes interviewers listen for 1. **"ListingsHelper only works in listings views."** That was the behaviour before `helper :all` became the default; today it is what you get only with the flag off. 2. **"Rails complains about duplicate helper methods."** It does not; the later module wins silently, which is why a clash surfaces as a wrong value or a `NoMethodError` raised inside the other module's method. 3. **"Turning the flag off breaks ApplicationHelper."** It does not; `ApplicationController` includes `ApplicationHelper` by name and every controller inherits it. 4. **"Controllers inherit helper methods."** Helpers are mixed into the **view context**, not the controller; actions reach them through `helpers`. A senior answer ties the setting to a decision: either keep the default and name helpers so they cannot clash, or turn it off and make every cross-controller dependency an explicit `helper` line.

  • How do you call a view helper such as number_to_currency from a controller action?
    Use the `helpers` proxy: `helpers.number_to_currency(order.total)`. In an action it returns the view context, so app helpers and Action View helpers are both reachable. The reverse direction, a controller method used in views, is `helper_method :current_cart`.
  • When would you move helper methods into a presenter or a component instead?
    When many display methods concern one model or one widget, they collide in the shared helper namespace and are awkward to test. A presenter wrapping the record, or a component with its own template, groups them with explicit inputs; small cross-cutting formatters can stay as helpers.

  • ListingsHelper is only available in ListingsController views by default
  • Rails raises an error when two helpers define the same method
  • include_all_helpers = false removes ApplicationHelper from other controllers' views
  • Controllers can call view helpers directly as their own methods
  • helper_method copies a helper module's methods into the controller