Module: InertiaJb

Defined in:
lib/inertia_jb.rb,
lib/inertia_jb/props.rb,
lib/inertia_jb/helper.rb,
lib/inertia_jb/handler.rb,
lib/inertia_jb/railtie.rb,
lib/inertia_jb/version.rb,
lib/inertia_jb/controller.rb

Overview

InertiaJb lets you declare Inertia.js props for your Rails frontend components inside view templates, using plain Ruby Hashes powered by the jb renderer.

:inertia is registered as a real Rails template format, so a *.inertia template is rendered by the ordinary Rails pipeline (and never wrapped in a layout). Its return value is a Props object carrying the template's last expression — a Hash of props — which Controller#render_to_body hands to inertia-rails' :inertia renderer. Every Inertia protocol feature (partial reloads, optional/always/deferred props, merging, shared data, key transforms) therefore works out of the box.

Defined Under Namespace

Modules: Controller, Helper Classes: Handler, Props, Railtie

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.bind_to_view(view, block) ⇒ Object

Binds a lazy-prop block to the view that declared it, so the block evaluates in the same view context as an eager prop.

Eager props are evaluated while the .inertia template renders, so their render(partial: ...) is the view helper — it returns jb data and never touches response_body. Lazy props (+defer+/+optional+/+always+/...) are different: inertia-rails resolves them later via controller.instance_exec(&block), i.e. in controller context, where the same render(partial: ...) binds to +ActionController+'s render and assigns response_body. inertia-rails then performs its own render(json:), and the already-set response_body raises AbstractController::DoubleRenderError.

Wrapping the block in view.instance_exec keeps every block — eager or lazy — running against the view, so render behaves identically everywhere. Controller-exposed helpers still work in the view (e.g. policy_scope, current_user via Pundit/Devise +helper_method+s); the only controller-only call that does not carry over is render_to_string — use +render(partial:, formats: [:html])+ instead, which returns the same String in a view.



76
77
78
79
80
# File 'lib/inertia_jb/helper.rb', line 76

def self.bind_to_view(view, block)
  return block if block.nil?

  proc { view.instance_exec(&block) }
end