If ActionCable isn't working properly in your environment, StimulusReflex cannot function.
Step one to any troubleshooting process should be "is it plugged in?"
rails generate channel test in your Rails project folder. This will ensure that your ActionCable setup has been initialized, although you should verify that in your
import 'channels' present.
Next, copy and paste the following into the two specified files, replacing their contents.
app/channels/test_channel.rbclass TestChannel < ApplicationCable::Channeldef subscribedstream_from "test"enddef receive(data)puts data["message"]ActionCable.server.broadcast("test", "ActionCable is connected")endend
If ActionCable is running properly, you should see
ActionCable is connected in your browser's Console Inspector and
Client is live in your server's STDOUT log stream.
You can feel free to remove both of these files after you're done, but leave
StimulusReflex.initialize() and share one ActionCable connection.
You might want to know the order in which your Reflexes are called, how long it took to process each Reflex or what the Reflex response payload contains. Luckily you can enable Reflex logging to your browser's Console Inspector.
There are two ways to enable client debugging in your StimulusReflex instance.
You can provide
debug: true to the initialize options like this:
You can also set debug mode after you've initialized StimulusReflex. This is especially useful if you just want to log the Reflex calls in your development environment:
By default, ActionCable emits particularly verbose Rails logger messages. You can optionally discard everything but exceptions by switching to the
warn log level, as is common in development environments:
config/environments/development.rb# :debug, :info, :warn, :error, :fatal, :unknownconfig.log_level = :warn
Alternatively, you can disable ActionCable logs at the framework level. This may improve performance, at the cost of not having ActionCable logs when you need them.
config/initializers/action_cable.rbActionCable.server.config.logger = Logger.new(nil)
If you're stuck working with legacy applications that impose constraints on your data attribute naming scheme, Stimulus and StimulusReflex give you a mechanism to provide an alternative schema.
In addition to controllerAttribute, actionAttribute and targetAttribute as defined by Stimulus, StimulusReflex adds the following: reflexAttribute, reflexPermanentAttribute, reflexRootAttribute and reflexDatasetAttribute.
You can update these values by providing your own schema to
In the above example, you have now configured your application to parse your DOM for
data-avenger attributes instead of
data-reflex attributes. 🦸