stimulate()
directly from inside of a Stimulus controller, StimulusReflex maps your requests to Reflex classes on the server. These classes are found in app/reflexes
and they inherit from ApplicationReflex
.test
method in the Example Reflex class. We refer to Reflex class methods which get called from the client as "Reflex Action methods".session
object, and if you're using the (default) Page Morph Reflexes, any instance variables that you set in your Reflex Action method will be available to the controller action before your page is re-rendered.users#show
page can call Example#increment
.current_user
or equivalent accessor inside your Reflex actions. First, you'll need to make sure that your Connection is "identified" by your current_user
. Since ActionCable is separate from the ActionController namespace, accessors need to be setup as part of your authentication process.session_id
before they authenticate, and then switch over to current_user
.identified_by :current_user
, you can delegate current_user
to your ActionCable Connection:current_user
from all of your Reflex classes, delegate it your ApplicationReflex:identified_by
accessors such as current_user
, it's important to remember that any ActiveRecord queries or associations you access will be cached by default, even across multiple Reflexes.current_user
or other Connection identifiers in templates or partials that will be rendered in a Reflex.connection
- the ActionCable connectionchannel
- the ActionCable channelrequest
- an ActionDispatch::Request
proxy for the socket connectionsession
- the ActionDispatch::Session
store for the current visitorflash
- the ActionDispatch::Flash::FlashHash
for the current requesturl
- the URL of the page that triggered the reflexparams
- an ActionController::Parameters
of the closest formelement
- a Hash like object that represents the HTML element that triggered the reflexreflex_id
- a UUIDv4 that uniquely identies each Reflexreflex
and process
are reserved words inside Reflex classes. You cannot create Reflex actions with these names.element
element
property contains all of the Stimulus controller's DOM element attributes as well as other properties like tagName
, checked
and value
. In addition, values
and the dataset
property reference special collections as described below.checked
and selected
which are booleans.<select multiple>
or a collection of checkboxes with the same name
will emit an additional values
property. In addition, the value
property will contain a comma-separated string of the checked options.element
property and the dataset
collection in your Reflex action. You can use the dot notation as well as string and symbol accessors.id
and data-id
are different attributes. id
can be used as a CSS selector, and it must be unique in your DOM. It does not show up in your dataset
accessor. data-id
cannot be used as a CSS selector, does not have to be unique in your DOM and is part of your dataset
. Similar names, entirely different concepts and functions.value
and data-value
. value
has meaning to the browser and it's what gets processed in a form submission by Rails. data-value
is just an arbitrary name that you made up, and it could have just as easily been data-principle
.element
accessor on every Reflex has two dynamic accessors, signed
and unsigned
which automatically unpack Global IDs stored in data attributes and converts them to model instances.rescue_from
to respond to an errors raised.reflex_id
reflex_id
.