Scott Watermasysk
Turbo Streams -- append_all
While adding revisions (auto-save and version history) to PhrontPage, I needed a way to add a new element to the page. Using turbo_stream#append,
turbo_stream#update,
etc. would work, but only if there is an element on the page with a known dom id. Eventually, I would like this item (a toast component) to be more generally available, so I did not want to need to have anything hardcoded on the page.
In addition to append, update, etc., there are matching methods append_all,
update_all,
etc., that allow targeting one or more items with more flexible query selectors.
Append to the targets in the dom identified with targets either the content passed in or a rendering result determined by the rendering keyword arguments, the content in the block, or the rendering of the content as a record append_all
In my case, the Toast is added to the page, displays, and then removes itself.
Hattip to Matt Swanson for highlighting the returning component for the turbo_stream response.
def render_success_toast
component = Admin::Toast::Component.new(text: "Your changes have been saved")
render turbo_stream: turbo_stream.append_all("body", component)
end