Live counter updates with Turbo Streams (likes, votes)

226
0

Counters (likes, votes, bookmarks) are classic UI glue. I keep the counter itself in a small partial with a stable id and update it via turbo streams on create/destroy. The controller can render a turbo_stream.replace of the counter plus (optionally) a replace of the button state. This gives instant feedback and also keeps multiple tabs in sync if you add broadcasts later. The important detail is to compute the counter server-side (e.g., post.likes.count) to avoid state drift. If you care about performance, move to counter caches (likes_count) and update that in the DB, but the UI pattern stays the same: one target, one partial, replace.