Opal Stimulus for Rails
Opal Stimulus is a Stimulus wrapper made with Opal (a source-to-source Ruby to JavaScript compiler) that allows you to write Stimulus controllers in Ruby instead of JavaScript (It works only with Rails).
Installation
Add this line to your Gemfile:
gem 'opal_stimulus'
Execute:
bundle install
rails generate opal_stimulus:install
Start application:
bin/dev
Basic Example
Here's a Hello World example with OpalStimulus. Compare with the original JavaScript example:
Ruby Controller:
# app/opal/controllers/hello_controller.rb
# new controllers will be automatically added to app/opal/controllers_requires.rb
# (ordered files load is not supported yet)
class HelloController < StimulusController
self.targets = ["name", "output"]
def greet
output_target.content = "Hello, #{name_target.value}!"
end
end
HTML:
<div data-controller="hello">
<input data-hello-target="name" type="text">
<button data-action="click->hello#greet">
Greet
</button>
<span data-hello-target="output">
</span>
</div>
Result
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/josephschito/opal_stimulus.
License
The gem is available as open source under the terms of the MIT License.