DEV Community

David Boureau
David Boureau

Posted on • Edited on • Originally published at alsohelp.com

3

How to add or remove a Stimulus controller

Article originally published here : https://alsohelp.com/blog/how-to-add-or-remove-a-stimulus-controller

How to add a Stimulus controller

bin/rails generate stimulus foo
Enter fullscreen mode Exit fullscreen mode

I don't always rely on Rails generator, I parcularly enjoy to write Rails migration file by myself.

However I advise to stick with the generator for Stimulus, I find it safer, to avoid spelling mistakes - remember that Rails relies on naming conventions to wire things together.

Now the Stimulus controller is generated as follow inside the app/javascript/controllers/index.js file :

// app/javascript/controllers/index.js

import { application } from "./application"

import HelloController from "./hello_controller.js"
application.register("hello", HelloController)

import FooController from "./foo_controller.js"
application.register("foo", FooController)
Enter fullscreen mode Exit fullscreen mode

How to remove a Stimulus controller

bin/rails destroy stimulus foo
Enter fullscreen mode Exit fullscreen mode

This command may fail, so check that lines were correctly removed inside app/javascript/controllers/index.js.

If not, then manually remove the two lines, and run bin/rails stimulus:manifest:update.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay