We've moved discussions to Discord

Using Stimulus Controllers in Administrate

Ben Scheirman
I'm trying to use a stimulus controller in a custom Administrate field (to load some data from Stripe).

I currently have it all working as a single <script> tag in my field's _form.html.erb, but I want to move this to a Stimulus controller.

Assuming that Stimulus is not included in administrate's js, I added this to app/javascripts/administrate/index.js:

import { Application } from 'stimulus'
import CourseOfferingsController from './controllers/course_offerings_controller'

const application = Application.start()
application.register('course-offerings', CourseOfferingsController)

console.log("Stimulus registered")
console.log("controllers:", application.controllers)

my controller is stubbed out like this:

import { Controller } from "stimulus"

export default class extends Controller {
    static targets = []

    initialize() {
        console.log("stim initialize")
    }

    connect() {
        console.log("stim connect")
    }

    test(e) {
        e.preventDefault()
        console.log("stimulus")
    }
}

When I load my page I do see the "Stimulus Registered" line, however the controllers array is empty. What am I doing wrong?
Ben Scheirman
Okay false alarm, I can see these logs (when I reference the name of my controller correctly!) but I'm still confused as to why the controllers array was empty above. Perhaps this is only active controllers and not registrations. 
Notifications
You’re not receiving notifications from this thread.