Module: InertiaJb::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/inertia_jb/controller.rb

Overview

Controller concern that renders *.inertia templates as Inertia responses.

There is no control-flow trickery here: an .inertia page goes through Rails' ordinary render pipeline and its return value — a Props object — comes back out of render_to_body. We recognise it there and re-enter with inertia:, which inertia-rails registered as a proper ActionController::Renderers renderer. Component resolution, shared data, PropsResolver/partial reloads, config.layout, SSR and the X-Inertia/Vary headers therefore all stay with inertia-rails.

Because the hand-off keys on the rendered result, default_render needs no hook of its own: Rails' ordinary implicit render finds the .inertia template, and explicit renders work the same way — render :new, render template: "other/page", render :edit, status: :unprocessable_entity.

The one thing that does have to stay out of the way is inertia-rails' config.default_render, which short-circuits default_render into render inertia: true before any template is consulted; apps therefore set it to false (its own default).

InertiaRails::Controller is already mixed into ActionController::Base by inertia-rails' engine, so we don't include it again.

Instance Method Summary collapse

Instance Method Details

#render_to_body(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/inertia_jb/controller.rb', line 29

def render_to_body(options = {})
  body = super
  return body unless body.is_a?(InertiaJb::Props)

  # Second pass: `:inertia` is handled by inertia-rails' renderer, which
  # returns a real String body, so this never recurses further.
  render_to_body(inertia: body.component(inertia_configuration), props: body.props)
end