skip to content

For a dating app's phone screens, how does Rails choose profiles/show.html+phone.erb over show.html.erb, and what happens when no phone file exists?

level: seniorimportance: should knowfreq 22%

answer

  1. the +variant part of the name
  2. request.variant feeds the lookup
  3. no variant file falls back
  4. format still outranks variant
  5. partials and layouts use the same details

basics

~20 s

When the controller sets request.variant to :phone, Rails' template lookup requests that variant and ranks show.html+phone.erb above show.html.erb; if no +phone file exists, the variant-less template matches and renders. With no variant set, +phone files are never chosen.

solid answer

~40 s

A variant is the `+name` segment of a template file name. The controller sets `request.variant` (a symbol or an array, set in the controller), and Action View adds it to the lookup's details. A file whose variant is in the requested list matches, and so does a file with no variant at all; ranking puts the named variant first. So `show.html+phone.erb` wins for phones, `show.html.erb` is the fallback when the phone file is missing, and an empty variant list matches only variant-less files. An array is a priority order. Format is ranked before variant, so a `+phone` file never beats the wrong format. Partials and layouts use the same lookup, so `_card.html+phone.erb` and `application.html+phone.erb` work too.

code

ruby · 12 lines
ruby
class ApplicationController < ActionController::Base
  before_action :set_phone_variant

  private
    def set_phone_variant
      request.variant = :phone if request.user_agent.to_s.match?(/iPhone|Android.+Mobile/)
    end
end

# app/views/profiles/show.html+phone.erb      -> phones
# app/views/profiles/show.html.erb            -> everyone else, and phones if the +phone file is removed
# app/views/layouts/application.html+phone.erb -> phone layout

go deeper

for a junior

Recognise the +phone part of a template name as a variant chosen by the controller.

for a middle

Explain how request.variant enters the lookup, the per-file fallback and the format-first ranking.

for a senior

Decide when variants beat responsive CSS, keep variant files thin, and test both variants so they do not drift.

for a principal

Weigh variants against separate front ends or native screens as the product's device strategy evolves, owning the maintenance cost.

## What a variant is A **variant** is an extra detail in Rails' template lookup, written after a plus sign in the file name: `profiles/show.html+phone.erb` is the `show` template, format `html`, variant `phone`, handler `erb`. It lets one controller action serve different markup for the same format, typically a stripped-down phone layout or a variant for an embedded web view, without new routes or actions. The value comes from `request.variant`, which the controller sets (often in a `before_action` that inspects the user agent or a session flag). It accepts a symbol or an array of symbols and is stored as an `ActiveSupport::ArrayInquirer`. Setting it is controller work; this question is about what Action View does with it. ## How matching works The lookup context turns the requested variants into an index where each requested value gets a rank and "no variant" is always accepted, ranked after them: | `request.variant` | Accepted variants, best first | |---|---| | not set (empty) | none, so only files without `+variant` | | `:phone` | `phone`, then no variant | | `[:phone, :tablet]` | `phone`, `tablet`, then no variant | Consequences worth stating in an interview: - **Fallback is automatic.** If `show.html+phone.erb` does not exist, `show.html.erb` renders. You only write phone files for screens that really differ. - **No variant means no variant files.** A desktop request never picks up `show.html+phone.erb` by accident. - **Arrays are priorities.** With `[:phone, :tablet]` and only a `+tablet` file, the tablet file wins over the plain one. - **Format comes first.** Candidates are ranked by format, then locale, then variant, then handler, so the variant only decides between files that already match the requested format. ## It applies to everything looked up The same details travel with every lookup during the request, so: 1. the layout can have `layouts/application.html+phone.erb`; 2. a partial can have `profiles/_photo_strip.html+phone.erb` while its siblings stay shared; 3. a missing variant anywhere falls back independently. This per-file fallback is what keeps variants maintainable: a phone template can be as small as one partial override. ## Production judgement - **Responsive CSS first.** Most phone differences are layout, which CSS handles in one template. Variants earn their place when the markup or the data shown really differs, such as a swipe deck instead of a grid of member cards. - **Duplication is the cost.** The Rails guides warn that many near-identical variant templates make views harder to maintain. Keep variant files thin and push shared parts into partials that both use. - **Test both paths.** A system or integration test per variant catches a phone template that drifted from its desktop twin, for example a form field missing from the phone version after a refactor. - **Name variants with plain words.** Variant names are matched up to the next dot, so `phone` and `native` work while a name containing a dot cannot be expressed in a file name. - **Watch caches keyed on the URL.** If a phone and a desktop page share a URL, anything that caches the whole response by URL must also vary on whatever sets the variant. ## A worked example The dating app has a grid of member cards on desktop and a one-at-a-time swipe deck on phones: - `app/views/discover/index.html.erb`: the grid of cards; - `app/views/discover/index.html+phone.erb`: the swipe deck; - `app/views/discover/_card.html.erb`: the shared card; - `app/views/layouts/application.html.erb`: the shared layout. A phone request renders `index.html+phone.erb`, the shared `_card.html.erb` (no phone file, so it falls back) and the shared layout. Adding `_card.html+phone.erb` later changes only the card on phones, with no controller change. ## Debugging a wrong pick With `annotate_rendered_view_with_filenames` on in development, the HTML comments show which file rendered. If a phone request shows the desktop page, check that the variant is set before rendering and that the file name puts the variant after the format (`show.html+phone.erb`, not `show+phone.html.erb`).

  • Why does `show+phone.html.erb` never render for a phone request?
    The file-name pattern expects the variant after the format: `name.format+variant.handler`. In `show+phone.html.erb` the parser reads the plus sign as part of the action name, so the file is not the `show` template at all.
  • When would you choose a variant over a separate format such as `:mobile`?
    A variant keeps the format `html`, so everything that keys on format, such as `respond_to` blocks, MIME types and helpers, keeps working, and missing variant files fall back. A custom format would need its own MIME registration and full template coverage, which is rarely what a phone layout needs.

  • Without a show.html+phone.erb file, a phone request raises MissingTemplate.
  • Desktop requests also pick up +phone templates when they are newer.
  • Variants replace formats, so a +phone file can serve a JSON request.
  • Only the action template honours the variant, not partials or layouts.
  • The variant goes before the format, as in show+phone.html.erb.