Quick reference

StimulusReflex Cheatsheet

Quick reference guide for StimulusReflex in Ruby on Rails, covering reflex actions, morph modes, lifecycle callbacks, and client events.

· Published

Core Concepts

StimulusReflex enables reactive, real-time user interfaces in Rails applications using WebSockets (ActionCable) and CableReady.

Basic Reflex

<!-- view.html.erb -->
<button data-reflex="click->Counter#increment">Increment</button>
# app/reflexes/counter_reflex.rb
class CounterReflex < ApplicationReflex
  def increment
    @count = element.dataset[:count].to_i + 1
  end
end

Morph Modes

Page Morph (Default)

Re-renders the entire page via controller action and morphs differences into the DOM.

Selector Morph

# app/reflexes/example_reflex.rb
class ExampleReflex < ApplicationReflex
  def update_metric
    morph "#metric-display", render(partial: "metrics/metric", locals: { value: 42 })
  end
end

Nothing Morph

Executes server-side logic without updating the client DOM.

class ExampleReflex < ApplicationReflex
  def background_task
    LongRunningJob.perform_later
    morph :nothing
  end
end

Lifecycle Methods

Server-side Callbacks

class ExampleReflex < ApplicationReflex
  before_reflex :authenticate
  around_reflex :measure_time
  after_reflex :log_activity
end

Client-side Generic Callbacks

  • beforeReflex(element, reflex, noop, reflexId): Before sending WebSocket request
  • reflexSuccess(element, reflex, noop, reflexId): After reflex execution and DOM update
  • reflexError(element, reflex, error, reflexId): When server raises an unhandled error
  • reflexHalted(element, reflex, noop, reflexId): When reflex is aborted with throw :abort
  • afterReflex(element, reflex, noop, reflexId): Executed on both success and failure
  • finalizeReflex(element, reflex, noop, reflexId): Final cleanup hook

DOM Events

  • stimulus-reflex:before
  • stimulus-reflex:success
  • stimulus-reflex:error
  • stimulus-reflex:halted
  • stimulus-reflex:after
  • stimulus-reflex:connected
  • stimulus-reflex:disconnected
  • stimulus-reflex:ready

Practical Patterns

Form Handling

Reflexes triggered inside or on form elements automatically serialize and pass inputs into params.

Promise Chaining

this.stimulate('Comments#create')
  .then(() => this.showSuccess())
  .catch((err) => this.handleError(err))

Permanent Elements

Mark elements with data-reflex-permanent so that full-page morphs leave them untouched.

<div data-reflex-permanent>
  <video id="player" src="video.mp4" controls></video>
</div>

References

Related topics