Stimulus generator for rails
This is generator for Stimulus on Rails application with Webpacker.
Installation
Add to your Gemfile:
gem 'stimulus-generator-rails'
Add this code to your webpacker's entrypoint javascript file:
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start()
const context = require.context("../javascripts/controllers", true, /\.js$/)
application.load(definitionsFromContext(context))
Usage
$ rails g stimulus CONTROLLER_NAME [action action]
# e.g.
$ rails g stimulus books index edit
Attention
This generator will create javascript and stylesheet into webpacker's source_entry_path/../javascripts/controllers
and source_entry_path/../stylesheets
.
So if you set source_entry_path
app/webpacker/entrypoint
, you will see file structure like this:
app/webpacker
├── entrypoint
│ └── application.js
├── images
├── javascripts
│ └── controllers
│ └── books
│ └── index_controller.js
└── stylesheets
└── books.scss