diff --git a/README.md b/README.md index d1addf4..cf26f9b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This gem is automatically configured for applications made with Rails 7+ (unless ### Installing with installer 1. Add the `stimulus-rails` gem to your Gemfile: `gem 'stimulus-rails'` -2. Run `./bin/bundle install`. +2. Run `./bin/bundle install` 3. Run `./bin/rails stimulus:install` The installer will automatically detect whether you're using an [import map](https://github.com/rails/importmap-rails) or [JavaScript bundler](https://github.com/rails/jsbundling-rails) to manage your application's JavaScript. If you're using an import map, the Stimulus dependencies will be pinned to the versions of the library included with this gem. If you're using Node, yarn will add the dependencies to your `package.json` file. @@ -24,7 +24,7 @@ The installer amends your JavaScript entry point at `app/javascript/application. Note that we recommend running the installer as described above. But the following is helpful if you encounter errors while running the installer (e.g., if there are conflicts with existing files) or if you just like doing stuff manually. Follow the instructions for import map *or* JavaScript bundler, depending on your setup. 1. Add the `stimulus-rails` gem to your Gemfile: `gem 'stimulus-rails'` -2. Run `./bin/bundle install`. +2. Run `./bin/bundle install` #### With import map @@ -65,16 +65,19 @@ pin_all_from "app/javascript/controllers", under: "controllers" #### With JavaScript bundler -3. Create `app/javascript/controllers/index.js` and load your controllers. +3. Create `app/javascript/controllers/index.js` and load your controllers: -Make sure to change `HelloController` to an actual controller and repeat for every controller you have or use the command mentioned in the comments below: ```javascript // This file is auto-generated by ./bin/rails stimulus:manifest:update // Run that command whenever you add a new controller or create them with // ./bin/rails generate stimulus controllerName import { application } from "./application" +``` + +You can also load you controllers manually like this: +```javascript import HelloController from "./hello_controller" application.register("hello", HelloController) ``` diff --git a/lib/install/app/javascript/controllers/hello_controller.js b/lib/install/app/javascript/controllers/hello_controller.js deleted file mode 100644 index 5975c07..0000000 --- a/lib/install/app/javascript/controllers/hello_controller.js +++ /dev/null @@ -1,7 +0,0 @@ -import { Controller } from "@hotwired/stimulus" - -export default class extends Controller { - connect() { - this.element.textContent = "Hello World!" - } -} diff --git a/lib/install/app/javascript/controllers/index_for_node.js b/lib/install/app/javascript/controllers/index_for_node.js index d0685d3..373c3ed 100644 --- a/lib/install/app/javascript/controllers/index_for_node.js +++ b/lib/install/app/javascript/controllers/index_for_node.js @@ -3,6 +3,3 @@ // ./bin/rails generate stimulus controllerName import { application } from "./application" - -import HelloController from "./hello_controller" -application.register("hello", HelloController) diff --git a/lib/install/stimulus_with_bun.rb b/lib/install/stimulus_with_bun.rb index e6250b8..a01b144 100644 --- a/lib/install/stimulus_with_bun.rb +++ b/lib/install/stimulus_with_bun.rb @@ -4,8 +4,6 @@ "app/javascript/controllers/index.js" copy_file "#{__dir__}/app/javascript/controllers/application.js", "app/javascript/controllers/application.js" -copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js", - "app/javascript/controllers/hello_controller.js" if (Rails.root.join("app/javascript/application.js")).exist? say "Import Stimulus controllers" diff --git a/lib/install/stimulus_with_importmap.rb b/lib/install/stimulus_with_importmap.rb index 170ee38..c4682a5 100644 --- a/lib/install/stimulus_with_importmap.rb +++ b/lib/install/stimulus_with_importmap.rb @@ -4,8 +4,6 @@ "app/javascript/controllers/index.js" copy_file "#{__dir__}/app/javascript/controllers/application.js", "app/javascript/controllers/application.js" -copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js", - "app/javascript/controllers/hello_controller.js" say "Import Stimulus controllers" append_to_file "app/javascript/application.js", %(import "controllers"\n) diff --git a/lib/install/stimulus_with_node.rb b/lib/install/stimulus_with_node.rb index 50431dc..ae9012c 100644 --- a/lib/install/stimulus_with_node.rb +++ b/lib/install/stimulus_with_node.rb @@ -4,8 +4,6 @@ "app/javascript/controllers/index.js" copy_file "#{__dir__}/app/javascript/controllers/application.js", "app/javascript/controllers/application.js" -copy_file "#{__dir__}/app/javascript/controllers/hello_controller.js", - "app/javascript/controllers/hello_controller.js" if (Rails.root.join("app/javascript/application.js")).exist? say "Import Stimulus controllers"