# Simplest way to achieve this? (Turbo Streams)

**URL:** <https://discuss.hotwired.dev/t/simplest-way-to-achieve-this-turbo-streams/4221>\
**Category:** General\
**Created:** [June 12, 2022, 3:46pm UTC](https://discuss.hotwired.dev/t/simplest-way-to-achieve-this-turbo-streams/4221 "2022-06-12T15:46:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bolandross](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/bolandross/32/2770_2.png) [@bolandross](https://discuss.hotwired.dev/u/bolandross)\
**Post date:** [June 12, 2022, 3:46pm UTC](https://discuss.hotwired.dev/t/simplest-way-to-achieve-this-turbo-streams/4221/1 "2022-06-12T15:46:48Z")

</div>

Dear community,

please excuse the amateur nature of my question. I’m totally new to Turbo, but also already intrigued by its possibilities and most importantly the low amount of code to write to achieve complex things.

I would like to start with a small goal, but for now I was unable to find a good tutorial or example to show me the basics without overloading the capacity of my brain.

Maybe you can help me with just a little hint on how one would achieve this the simplest way?

In a fresh Rails 7 app I have a list of people names that is shown like this:

 ![01](https://canada1.discourse-cdn.com/flex027/uploads/stimulusjs/original/2X/4/458683a7db477076b5def7bd0755564cd1281b5d.png)

my **index.html.erb** looks like this:

```auto
<div id="people">
	<h1>People</h1>
	<table width=200>
		<tbody>
			<% @people.each do |person| %>
			<tr>
				<td><%= person.name %></td><td><%= link_to "Details", person %></td>
			</tr>
			<% end %>
		</tbody>
	</table>
	<p><%= link_to "New person", new_person_path %></p>
</div>

```

I would like this page to automatically update for all visitors, as soon as the list of names is changed via Rails console. Can you help me and give me a hint on which changes would be necessary to achieve this?

Thank you so much in advance for your patience to help!

Best regards

---

<div class="post-metadata">

**Author:** ![yunggindigo](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/yunggindigo/32/1271_2.png) [@yunggindigo](https://discuss.hotwired.dev/u/yunggindigo)\
**Post date:** [June 13, 2022, 8:59pm UTC](https://discuss.hotwired.dev/t/simplest-way-to-achieve-this-turbo-streams/4221/2 "2022-06-13T20:59:06Z")

</div>

You probably would want to extract the row for each person into a partial then you can replace it whenever the record is updated  
Add a [dom\_id](https://apidock.com/rails/ActionView/RecordIdentifier/dom_id) to make a unique id for the record

```auto
 # people/_person.html.erb
<tr id="<%= dom_id(person) %>">
  <td><%= person.name %></td><td><%= link_to "Details", person %></td>
</tr>

```

You also need to make a turbo-stream-connection in the view

```auto
# index.html.erb
<%= turbo_stream_from :people %>

```

```auto
# models/person.rb
after_update_commit :broadcast_person

def broadcast_person
  broadcast_update_to(:people)
  # it will use the person partial by default
end

```

Because of the dom\_id when the partial is broadcasted to people it will replace the old one

---

<div class="post-metadata">

**Author:** ![bolandross](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/bolandross/32/2770_2.png) [@bolandross](https://discuss.hotwired.dev/u/bolandross)\
**Post date:** [June 13, 2022, 9:56pm UTC](https://discuss.hotwired.dev/t/simplest-way-to-achieve-this-turbo-streams/4221/3 "2022-06-13T21:56:18Z")

</div>

Hey and thank you very much for your help!

I followed your recommendations and it helped me to get some first feelings of success 😅

When changing names, the site now automatically updates!

Only when creating or destroying entries, it doesn’t keep up. I tried `after_create_commit` and `after_destroy_commit` for the model, but unfortunately without success. Do I have to do something different here?

Thanks!

---

<div class="post-metadata">

**Author:** ![yunggindigo](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/yunggindigo/32/1271_2.png) [@yunggindigo](https://discuss.hotwired.dev/u/yunggindigo)\
**Post date:** [June 13, 2022, 11:39pm UTC](https://discuss.hotwired.dev/t/simplest-way-to-achieve-this-turbo-streams/4221/4 "2022-06-13T23:39:31Z")

</div>

You can either switch to using broadcasts\_to to define after create, update, and destroy for you

```auto
# person.rb
broadcasts_to -> (person) { :people }

```

Or you can write different broadcast methods for each callback manually  
All of the available actions are defined here in the [code](https://github.com/hotwired/turbo-rails/blob/main/app/models/concerns/turbo/broadcastable.rb) and there is inline documentation

```auto
after_destroy_commit { broadcast_remove_to(:people) }
after_create_commit { broadcast_append_to(:people) }

```

---

<div class="post-metadata">

**Author:** ![bolandross](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/bolandross/32/2770_2.png) [@bolandross](https://discuss.hotwired.dev/u/bolandross)\
**Post date:** [June 14, 2022, 7:26am UTC](https://discuss.hotwired.dev/t/simplest-way-to-achieve-this-turbo-streams/4221/5 "2022-06-14T07:26:23Z")

</div>

Thank you!

It seems like there is a lot left to study for me. For now, this is enough help to assist me on my way. Thanks for your help and patience!
