June 2025
Intermediate to advanced
488 pages
12h 27m
English
Real-time updates require WebSockets, JavaScript, and a bit of server code to make it all work. Fortunately, Rails has a feature called Hotwire that makes this easy.
First, we add the following line to the app/models/product.rb file:
| | class Product < ApplicationRecord |
| | has_one_attached :image |
| » | after_commit -> { broadcast_refresh_later_to "products" } |
| | end |
This line tells Rails to broadcast changes to the product model to any clients that are listening. We also need to add the following lines to the top of the app/views/products/index.html.erb file:
| » | <%= turbo_stream_from "products" %> |
| » | <%= ... |
Read now
Unlock full access