In a Rails controller, what does an action render when it never calls render or redirect_to, and how do its instance variables reach the view?
answer
- convention over configuration
- controller_path plus action_name
- default_render runs unless performed?
- view_assigns copies instance variables
- one controller instance per request
basics
~10 sAn action that ends without rendering gets the template named after its controller and action, such as app/views/vendors/index.html.erb, and every instance variable the controller set is copied into that view; local variables are not.
solid answer
~40 sRails dispatches every request to a **new controller instance** and calls the public method the route names. When the method returns, `send_action` checks `performed?`; if nothing rendered, redirected or sent `head`, it calls `default_render`, which renders the template for that action in the requested format — `app/views/vendors/index.html.erb` for an HTML request to `VendorsController#index`. Before the template runs, `view_assigns` collects every instance variable on the controller, whether set in the action or in a `before_action`, minus Rails' internal ones such as `@_params`, and assigns them to the view. Locals die with the method, so `@vendors = Vendor.all` reaches the template and `vendors = Vendor.all` does not. An action can be an empty method, and if a template exists the method can even be missing.
code
ruby · 13 linesclass VendorsController < ApplicationController
before_action :set_market
def index
@vendors = @market.vendors.order(:stall_number)
open_count = @vendors.where(open: true).count # local: never reaches the view
end
private
def set_market
@market = Market.find(params.expect(:market_id))
end
endgo deeper
Know the path convention, controller folder plus action name plus format, and say plainly that instance variables reach the template while locals do not.
Explain the mechanism: send_action checks performed?, default_render renders the action's template, and view_assigns copies the controller's instance variables minus Rails' protected ones.
Point out the per-request controller instance and what really persists between requests, and spot code that leans on dozens of instance variables where a presenter or explicit locals would be clearer.
Weigh the convention's payoff, almost no rendering code, against its cost: implicit data flow from controller to view that a team must keep readable through naming discipline and review.
## The convention Rails names things so that most actions need no rendering code at all. A **controller** is a class such as `VendorsController < ApplicationController`; an **action** is one of its public methods, such as `index`; a **template** is a file under `app/views` named after the controller path and the action, plus the format and the template handler: `app/views/vendors/index.html.erb`. When the action does not say what to send back, Rails renders that template. This is the **implicit render**, and it is why a scaffolded `show` action is an empty method. ## What happens after the action method returns 1. The router maps the request to a controller and an action name. 2. `ActionController::Metal.dispatch` builds a **new instance** of the controller for this one request. 3. The `before_action` and `around_action` callbacks run, then `send_action` calls the action method. 4. `ActionController::BasicImplicitRender#send_action` checks `performed?` — true once `render`, `redirect_to`, `head` or a send method has set a response body. 5. If nothing was performed, `default_render` runs. In a full `ActionController::Base` controller it renders the template for `action_name` in the request's format and variant. 6. Rendering builds a view context and hands it the controller's instance variables. The template lookup itself — prefixes, partials, layouts and variants on disk — is the view layer's business; the controller only decides that the action's own template is what gets rendered. ## How instance variables travel to the view - `AbstractController::Rendering#view_assigns` reads `instance_variables` on the controller, removes the protected internal ones (`@_params`, `@_response`, `@_request`, `@_action_name` and similar), and builds a hash of the rest. - The view context receives that hash and sets each entry as an instance variable of its own, so `@vendors` in the controller is `@vendors` in ERB. - The **objects are shared, not copied**: mutating `@vendors` in the template mutates the same relation, but reassigning `@vendors` in the template does not change the controller's variable. - Instance variables set in a `before_action`, such as a `set_market` loader, arrive too, because callbacks and the action run on the same instance. - **Local variables never arrive.** They belong to the method's scope and are gone when it returns. - Nothing leaks between requests: the instance is thrown away after the response, so `@vendors` from one request cannot appear in the next. Class-level state is a different story and is shared by every request in the process. ## Edge cases | Situation | What Rails sends | |---|---| | Empty `def show; end` and `show.html.erb` exists | The template, with any variables set by callbacks | | No `show` method at all, but `show.html.erb` exists | The template: `ImplicitRender#method_for_action` treats a template as an action | | The action calls `render` or `redirect_to` itself | Whatever it asked for; the implicit render is skipped because `performed?` is true | | No template for the request at all | A 406 or an empty 204, depending on the request — a separate set of rules | ## Why interviewers start here The question looks simple, but the answer shows whether a candidate has a working model of a request. Three facts carry most of the weight: - **The method is not the response.** The action's return value is ignored; what matters is whether something set the response before the method returned. - **The template is chosen by convention, not by the action.** Controller path, action name and the request's format pick the file, so renaming an action without renaming its template breaks the page. - **State is per request.** The controller is a short-lived object, which is why instance variables are a safe hand-off to the view and an unsafe cache. A candidate who can say those three things can usually reason through the follow-ups — missing templates, multiple formats, shared helpers — without having memorised them. ## A worked example A farmers-market vendor list sets two instance variables and one local. Only the two instance variables are visible in `app/views/vendors/index.html.erb`; `open_count` is computed and discarded. ## Common mistakes - Expecting a local to show up in the template, then "fixing" it with a global or a constant. - Believing the controller object survives between requests and caching per-user data in an instance variable "for next time". - Calling `render :index` at the end of `index` out of habit. It works, but it repeats what the convention already does and hides the cases where a different template really is rendered. - Assigning dozens of instance variables. Every one becomes view state; a template that needs many is usually asking for a presenter object or a partial with explicit locals. Knowing exactly when the implicit render fires also explains its failures: when no template matches, the same `default_render` method decides between an error and an empty response.
- What happens if a route points at a Rails action that has a template but no method in the controller?The request still succeeds. `ImplicitRender#method_for_action` falls back to `default_render` when a template exists for the action name, so Rails renders that template. Callbacks still run, so instance variables set in a `before_action` are available, but there is no action body to set anything else.
- Why can an instance variable from one Rails request never show up in the next request's view?`ActionController::Metal.dispatch` calls `new` for every request, so each request has its own controller object and its own instance variables, which are discarded with the object. Data that does persist across requests lives elsewhere: the session, the cache, the database, or class-level state, which is shared by every request in the process and is a common source of leaks.
- Does a Rails view get a copy of the controller's objects or the same objects?The same objects. `view_assigns` builds a hash of names to the controller's objects and the view sets them as its own instance variables. Calling a mutating method on `@vendors` in the template affects that object, but assigning a new value to `@vendors` in the template only changes the view's variable.
- An action must end with an explicit render call or the browser gets a blank page.
- Local variables in the action are visible in the template just like instance variables.
- The controller object is reused across requests, so instance variables persist between them.
- Instance variables set in a before_action do not reach the view; only the action's do.
- A route to an action without a controller method always fails, even when the template exists.