skip to content

In a Rails controller, why does an update action that calls a private guard method doing redirect_to ... and return still raise AbstractController::DoubleRenderError?

level: seniorimportance: should knowfreq 50%

answer

  1. return leaves only the current method
  2. response_body already set
  3. render, redirect_to and head all check
  4. performed? tells you it happened
  5. implicit render checks first

basics

~20 s

The return inside the guard exits only the guard method. The action keeps running to its own redirect_to or render, which finds response_body already set and raises AbstractController::DoubleRenderError. Check performed? after the guard, or make the guard a before_action.

solid answer

~40 s

`render`, `redirect_to` and `head` each start by raising `AbstractController::DoubleRenderError` if `response_body` is already set, and none of them stops the method that called them. `redirect_to closed_path and return` works inside an action because `redirect_to` returns a truthy value and `return` leaves the action. Put that line in a private helper such as `reject_if_closed` and `return` leaves only the helper; the action carries on to its own `redirect_to @ticket` and raises. Fix it at the call site with `reject_if_closed; return if performed?`, or turn the guard into a `before_action`, whose chain halts once a callback has rendered or redirected. Rails' implicit render already checks `performed?`, so a lone `render` inside an `if` with nothing after it is safe.

code

ruby · 18 lines
ruby
class TicketsController < ApplicationController
  before_action :set_ticket
  before_action :reject_if_closed, only: %i[edit update]

  def update
    if @ticket.update(ticket_params)
      redirect_to @ticket, status: :see_other
    else
      render :edit, status: :unprocessable_content
    end
  end

  private

  def reject_if_closed
    redirect_to @ticket, alert: "Ticket is closed." if @ticket.closed?
  end
end

go deeper

for a junior

Remember that render and redirect_to do not end the action, and that a second call raises AbstractController::DoubleRenderError. Add return after an early answer.

for a middle

Explain the response_body check in render, redirect_to and head, what performed? reports, why and return works inside an action, and why a helper's return does not leave it.

for a senior

Show you restructure guards into before_action filters or exceptions with rescue_from, and spot helper-level returns and fall-through head calls in review.

for a principal

Treat one-answer-per-path as a code-review convention: guards as filters, actions short and branch-complete, and shared rules raised and mapped centrally.

## The rule behind the error An Action Controller action answers **once**. Three methods produce the answer, and each guards against a second one: - `render` (any form: a template, `plain:`, `html:`, `body:`, `inline:`) - `redirect_to` (and `redirect_back_or_to`, which calls it) - `head` Each begins with `raise AbstractController::DoubleRenderError if response_body`. `redirect_to` and `head` set `response_body` to an empty string; `render` sets it to the rendered output. The error message spells out the trap: neither redirect nor render will "terminate execution of the action". The controller also exposes `performed?`, which is true once `response_body` is set (or the response was committed). Rails' **implicit render** at the end of an action runs only when `performed?` is false, which is why an action may render inside an `if` with no `else` and no error. ## The helpdesk bug A support agent may not edit a closed ticket. The guard was extracted into a helper: ```ruby def update reject_if_closed if @ticket.update(ticket_params) redirect_to @ticket, status: :see_other # raises for closed tickets end end private def reject_if_closed redirect_to @ticket, alert: "Ticket is closed." and return if @ticket.closed? end ``` For an open ticket, nothing happens in the guard and the action redirects once. For a closed ticket: 1. `reject_if_closed` calls `redirect_to`, which sets the status, `Location` and `response_body`. 2. `and return` leaves **`reject_if_closed`**, the method it is written in. It cannot leave `update`. 3. `update` continues, the ticket saves (another bug), and its own `redirect_to` sees `response_body` and raises `AbstractController::DoubleRenderError`. ## Why `and return` works where it does `redirect_to url and return` is ordinary Ruby: `and` has very low precedence, so the line is `(redirect_to url) and return`. `redirect_to`, `render` and `head` all return a truthy value, so the `return` always runs. What `return` cannot do is leave a method other than the one it is written in: - Inside the action, it ends the action, which is what you want. - Inside a helper the action calls, as above, it ends only the helper. - Inside a block passed to another method, such as `respond_to`, it returns from the enclosing action, because blocks return from the method they were defined in; a lambda would not. ## Fixes, from local to structural | Fix | How it reads | Notes | |---|---|---| | Check after the call | `reject_if_closed; return if performed?` | smallest change; easy to forget at each call site | | Return a boolean | `return unless editable_ticket?` with the redirect inside | explicit, but mixes a query with a side effect | | Use a filter | `before_action :reject_if_closed, only: %i[edit update]` | the callback chain halts once a callback has rendered or redirected, so `update` never runs | | Raise and rescue | raise a domain error, map it with `rescue_from` | good when many actions share the rule | The filter is the usual answer: Action Controller's callback terminator checks `performed?` after each `before_action`, so a redirect in the filter ends the chain before the action body. A `return` is still useful inside an action when a response is produced **in the middle** of it and more code follows. ## Variants of the same error - **Two branches that both answer**: a `render` inside `if` followed by an unconditional `render` after it. - **A loop or `each` that renders** for the first matching item and keeps iterating. - **`head` after `render`**, for example a webhook action that renders an error and then falls through to `head :ok`. - **`respond_to` blocks plus a later render** in the same action. In each case the cure is one answer per code path: `return` right after the call, an `else` branch, or `performed?` where the response may already exist. ## What the error does not mean - It is not raised by the view; it fires in the controller at the second call. - It does not mean the first response reached the client: the request ends with an error response. - It is unrelated to Turbo or the browser: it is a guard inside Action Controller itself.

  • What does performed? report in a Rails controller, and where is it useful?
    `performed?` is true once `render`, `redirect_to` or `head` has set the response body, or the response was committed. Rails itself checks it twice: the callback chain halts when a `before_action` leaves it true, and the implicit render is skipped when it is true. In your own code it belongs after a call to a helper that might already have answered.
  • How does Rails avoid a double render when an action renders conditionally and then ends?
    After the action method returns, Rails calls its implicit render only `unless performed?`. If the action already called `render`, `redirect_to` or `head`, `response_body` is set, `performed?` is true and no default template is rendered.

  • and return inside a helper method also ends the calling action
  • redirect_to stops the action, so code after it never runs
  • the implicit render at the end of an action raises DoubleRenderError
  • head is exempt from the render-once rule because it sends no body
  • performed? only turns true after a template has been rendered