This project adds a new Rails generator to create new Stimulus controllers in your app/javascript/controllers directory.
Add the gem to your Gemfile
gem 'rails-stimulus-generator'
And bundle install
rails g stimulus Hello
Creates app/javascript/controllers/hello_controller.js
The generated Stimulus controller will look like this:
// app/javascript/controllers/hello_controller.js
import { Controller } from 'stimulus'
export default class extends Controller {
static targets = []
static classes = []
static values = {}
initialize() {
// Called once, when the controller is first instantiated
}
connect() {
// Called any time the controller is connected to the DOM
}
disconnect() {
// Called any time the controller is disconnected from the DOM
}
}
This generator assumes that your Stimulus controllers live in app/javascript/controllers/
. If the controllers directory does not exist,
the generator will create the directory when it runs.
Clone this repository to your local machine and run rake test
The basic structure of this generator was inspired by stimulus-generator-rails. My preference is to structure my controllers by function instead of by action, so I created this gem for those with a similar need and a standard Rails 6 + webpack directory structure.
This gem is available as open source under the terms of the MIT License.