Updating Multiple Elements with Turbo Frames in Rails

Turbo Frames and Stimulus, integral components of Hotwire, fortify Rails applications with the capability to refresh specific segments of a web page dynamically. This approach obviates the need for complete page reloads, thereby enhancing user experience through seamless interactions. In this article, we’ll explore how Turbo Frames and Stimulus synergize to update multiple elements on a page in response to a single action, fostering interactivity in Rails applications.

Turbo and Stimulus Integration

Hotwire, a modern approach to building web applications in Rails, integrates Turbo and Stimulus seamlessly. Turbo facilitates page updates via Turbo Frames, while Stimulus augments interactivity by enabling behavior-driven enhancements within these frames.

Turbo Streams

The Turbo::StreamsHelper module in Rails provides developers with utilities for generating Turbo Stream actions. The turbo_stream method yields a Turbo::Streams::TagBuilder object, empowering developers to construct Turbo Stream actions for targeted updates of page elements.

Implementing Turbo Stream Actions

Consider a scenario where we aim to update two elements on the page upon the removal of an item from the shopping cart. We’ve crafted a Turbo Stream template named remove_item.turbo_stream.erb, which exhibits the following structure:

				
					<%= turbo_stream.update(:cartwrapper, partial: "cart/cart") %>
<%= turbo_stream.update(:cartvalue, partial: "shared/cart_counter", locals: { cart_items_count: @cart.line_items.count }) %>
				
			
  • The first line employs turbo_stream.update to refresh the element identified by the ID cartwrapper. It renders the partial _cart.html.erb, encapsulating the contents of the shopping cart.
  • The second line updates the element specified by the ID cartvalue and renders the partial _cart_counter.html.erb. It also passes the cart_items_count variable, representing the count of items in the shopping cart.

Controller Action

Within the controller action responsible for item removal from the cart, we integrate the turbo_stream format into the response. For instance:

				
					def remove_item
  # Logic to remove the item from the cart
  @cart.remove_item(params[:item_id])

  respond_to do |format|
    format.turbo_stream # Responding with Turbo Stream format
  end
end
				
			
  • The remove_item action orchestrates the removal of the specified item from the cart.
  • We respond to the request with the turbo_stream format, indicating that the ensuing response should be treated as a Turbo Stream action.

Leveraging Turbo Frames alongside Turbo Stream actions empowers Rails developers to build dynamic and engaging web applications effortlessly. Updating multiple elements on a page simultaneously in response to single actions greatly enhances user interaction and creates a responsive user interface.

1 comment on “Real-time Multiple elements update with Turbo Streams

  1. This is so informative, and will make a world of difference in the experience of my users! Thank you for this informative article.

Leave a Reply

Your email address will not be published. Required fields are marked *