A quick memo about how to add or remove a Stimulus controller - I recommend to rely on generator this time.

· Ruby  · 1 min read

How to add or remove a Stimulus controller

A quick memo about how to add or remove a Stimulus controller - I recommend to rely on generator this time.

How to add a Stimulus controller

bin/rails generate stimulus foo

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);

How to remove a Stimulus controller

bin/rails destroy stimulus foo

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.

Share:
Back to Blog

Related Posts

View All Posts »
The simplest turbo-frame example

The simplest turbo-frame example

Turbo frame is a powerful feature of Hotwire, here is a quick memo about how to follow conventions in the simplest case. Convention over configuration is powerful, but sometimes it is also a problem when you are not sure anymore about conventions ;) so I plan to release more "simplest *** Hotwire feature".

My honest opinion about Hatchbox

My honest opinion about Hatchbox

Hatchbox.io is a deployment platform for Ruby-on-Rails users. I used it recently, so for people ashamed of trying, here is my review.