Hotwire Discussion

Stimulus controller generator


I installed stimulus on Rails app. How can I generate Stimulus contoller. I saw it one tutorial but I can’t find it.

As I remember there is no such thing. It generates hello_controller.js only on installation step.

Take this as a start point :slight_smile:

// Visit The Stimulus Handbook for more details
// This example controller works with specially annotated HTML like:
// <div data-controller="hello">
//   <h1 data-target="hello.output"></h1>
// </div>

import { Controller } from "stimulus"

export default class extends Controller {
  static targets = [ "output" ]

  connect() {
    console.log('hello controller connected');
    this.outputTarget.textContent = 'Hello, Stimulus!'