Use dom_id everywhere to keep Turbo replacements deterministic

3281
0

Turbo Stream updates are easiest when every meaningful element has a stable id. Rails’ dom_id helper makes this painless: dom_id(@post) yields post_123, and dom_id(@post, :header) yields header_post_123. I use this pattern throughout Hotwire UIs so I can reliably target elements for replace, remove, and update. It also makes testing easier (assert_selector '#post_123'). The biggest benefit is avoiding brittle hand-made IDs and ensuring consistency across partials. I recommend deciding a convention early (wrapper div per record) and sticking to it. Once you do, Turbo Streams become a set of simple, composable DOM operations instead of “find and mutate” spaghetti.