This is a sample of how to use ActionCable & Stimulus to update partials in Rails 6.
- config/cable.yml - I setup the local adaptor to be
redis
so ActionCable will work locally. - app/javascript/controllers/realtime_partial_controller.js - The Stimulus controller for making a partial update when being updated via ActionCable.
- app/views/comments/index.html.erb - The adjustment to the view I added to make the Stimulus controller activate.
- app/jobs/partials/comments/list_job.rb - The ActiveJob where I broadcast the updated partial via ActionCable.
- test/system/comments/realtime_partials_test.rb - The system test for the realtime partials.
Pull down the repo & run:
brew install redis
./bin/setup
brew install redis
You will need redis installed on your local machine & have it running for ActionCable to work.bin/setup
- runs bundler & yarn, then sets up the database.
Next you should be able to run:
rails s
To turn on the server, your port may vary but if you visit http://localhost:3000/comments you should be able to see the realtime updates when CRUD'ing objects.