Skip to content

Commit

Permalink
Add a stimulus JS hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexB52 committed Apr 20, 2024
1 parent 090a32c commit e43010e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See some examples of how the UniRails library can be used
- [Todos app (JSON API)](/examples/todos-api.rb)
- [Todos app (Rails scaffold)](/examples/todos-scaffold.rb) based off `bin/rails g scaffold todo name completed_at:datetime`
- [Todos app (Hotwire)](/examples/todos-hotwire.rb) based off online article: [turbo-rails-101-todo-list](https://www.colby.so/posts/turbo-rails-101-todo-list) by David Colby
- [App using StimulusJS](/examples/stimulus-app.rb)
- [App using Puma server](/examples/server-puma-app.rb)
- [App using Falcon server](/examples/server-falcon-app.rb)

Expand Down
43 changes: 43 additions & 0 deletions examples/stimulus-app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Run the application
# $ ruby hello-world.rb

ENV['SECRET_KEY_BASE'] = 'my_secret_key_base'

require "bundler/inline"

gemfile(true) do
source "https://rubygems.org"
gem 'uni_rails'
end

require 'uni_rails'

UniRails::App.routes.append do
root 'pages#index'
end

UniRails.import_maps(
'stimulus' => 'https://unpkg.com/@hotwired/stimulus/dist/stimulus.js'
)

UniRails.javascript <<~JAVASCRIPT
import { Application, Controller } from "stimulus"
window.Stimulus = Application.start()
Stimulus.register("hello", class extends Controller {
connect() {
console.log("hello world")
}
})
JAVASCRIPT

class PagesController < ActionController::Base
layout 'application'
def index;end
end

UniRails.register_view "pages/index.html.erb", <<~HTML
<div data-controller="hello">Check out the dev console to see "hello world"</div>
HTML

UniRails.run(Port: 3000)

0 comments on commit e43010e

Please sign in to comment.