# How to delete turbo stream from another model's show page?

**URL:** <https://discuss.hotwired.dev/t/how-to-delete-turbo-stream-from-another-models-show-page/3330>\
**Category:** General\
**Created:** [October 4, 2021, 10:54pm UTC](https://discuss.hotwired.dev/t/how-to-delete-turbo-stream-from-another-models-show-page/3330 "2021-10-04T22:54:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cabrace](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/cabrace/32/2259_2.png) [@cabrace](https://discuss.hotwired.dev/u/cabrace)\
**Post date:** [October 4, 2021, 10:54pm UTC](https://discuss.hotwired.dev/t/how-to-delete-turbo-stream-from-another-models-show-page/3330/1 "2021-10-04T22:54:14Z")

</div>

Hey there, I’ve been stuck on what I would imagine to be a very simple thing to do, however it is not working for me and it would be awesome to have a pointer in the right direction.

Basically I’m just following the general _Hotwire Demo_ setup using default scaffolding changing the model names, and using Webpacker. I am just trying to delete an **Item** turbo\_stream element with an associated **dom\_id** of (item\_id#) which is just being displayed on the Show page of a **List** view.

I am thinking that on the List show page when deleting from the Item stream, turbo\_stream should be able to detect the item\_id# and remove the corresponding item from the item controller…However it for some reason not linking correctly and I’m not sure what to do…

Basically - How do I grab the **item\_id** from the -_items_ _turbo\_stream_- which is on the **list#show** page so that I can use that info on the _button\_to :delete_ method to tell the **ItemsController** which specific item needs to be deleted? I am hoping the TurboStream should be able to detect the **item\_id** to pass to the item controller to be removed…but how do I do this??

. Here is the setup below

The setup:  
Ruby 2.7.1  
Rails: 6.1.4.1

Models

```auto
#../models/item.rb
class Item < ApplicationRecord
  belongs_to :list

  broadcasts_to ->(i) { "items" }, inserts_by: :prepend, target: "items"
  after_destroy_commit -> { broadcast_remove_to self }
end

#../models/list.rb
class List < ApplicationRecord
  has_many :items , dependent: :destroy
  
  #broadcasts
  broadcasts_to ->(l) { "lists" }, inserts_by: :prepend, target: "lists"

  after_destroy_commit do
    broadcast_remove_to self
  end
end

```

Controllers:

```auto
#this one works for me fine on the list Index page
#../controllers/lists_controller.rb 
  # DELETE method
  def destroy
    @list.destroy
    respond_to do |format|
      format.turbo_stream { }
      format.html { redirect_to lists_url, notice: "List was successfully destroyed." }
      format.json { head :ok } 
    end
  end

#../controllers/items_controller.rb
  # DELETE /items/1 or /items/1.json
  def destroy
    @item.destroy

    respond_to do |format|
      format.turbo_stream { " " }
      #format.turbo_stream { render turbo_stream: turbo_stream.remove(@list.item) }
      #format.html { redirect_to items_url, notice: "Item was successfully destroyed." }
      format.html { redirect_to @list, notice: "Item was successfully destroyed." }
      format.json { head :no_content }
    end
  end

```

Views

```auto
#../views/lists/show.html.erb
...
...
<%= turbo_stream_from "items" %>
<div id="items" class="box">
<%= render @list.items %>
</div>

#../views/items/_item.html.erb
<div id="<%= dom_id item %>" >
  <div class="is-size-6 has-text-primary columns">
    <div class="column"><%= item.created_at.to_s(:short) %> : <%= item.name %></div>
    <div class="column"><%= button_to "Delete",<????>, method: :delete, class: "button is-danger" %></div>
  </div>
</div>

```

Right here I want to delete an **Item** _turbo\_stream_ from the **lists#show** page; however I’m not sure how to set the params in the button\_to :delete method. Any suggestions? Where am I to get the @list.item\_id params from to pass to the button??

---

<div class="post-metadata">

**Author:** ![cabrace](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/cabrace/32/2259_2.png) [@cabrace](https://discuss.hotwired.dev/u/cabrace)\
**Post date:** [October 7, 2021, 6:28pm UTC](https://discuss.hotwired.dev/t/how-to-delete-turbo-stream-from-another-models-show-page/3330/2 "2021-10-07T18:28:08Z")

</div>

OK - Lesson learned!  
No response can be a good thing as it is great to be able to answer your own questions at times!

The offending sections were :

**1.**

```auto
# ../views/items/_item.html.erb
<%= turbo_frame_tag item do %>
# Having to wrap the elements with the **turbo_frame_tag** instead of using the dom_id
<% end %>

```

The above was strange as the list#index page used:

```auto
<%= tag.div id: dom_id(list) do %>
/views/lists/_list.html.erb
# This worked for the lists route and was able to delete lists from the index page fine
# However the one above like the turbo_frame_tag
<% end %>

```

I had imagined that I could use Stream independently w/o the need for a turbo\_frame\_tag  
as the turbo\_frame\_tag was just used to interchange views. More testing to do on that.  
_Didn’t realize I may have needed a turbo\_frame\_tag here?_ I thought they were entirely separate structures.

**2.**  
The next offender was:

```auto
# /views/items/_item.html.erb
<%= turbo_frame_tag item do %>
# ....
# ....
    <div class="column"><%= button_to "Delete", **[@list, item]**, method: :delete, class: "button is-danger" %></div>
<% end %>

```

Here I needed to pass some variables back to the controller and **grab those variables** in the controller, assign to the **Item** model in the List controller to make appropriate destroy call like:

```auto
#../controllers/items_controller.rb  
  def destroy

    pp "PARAMS : #{params}" #Show params being passed so I can make the link clearer
    
    @item = Item.find(params[:id]) # Key 
    @item.destroy
    respond_to do |format|
      format.turbo_stream { render turbo_stream: " " }
      format.html { redirect_to @list, notice: "Item removed" }
      format.json { head :no_content }
    end
  end

```

That’s about it. Now I can have a List that has many Items, where Items from the list#show can be deleted from the turbo stream appropriately using the button\_to to pass Item model information through.

Oh Also: I changed the routes to expose the Item path…this feels wrong/hacky but it works for now.

```auto
#config/routes.rb

  resources :lists do
    resources :items
  end
  #resources :lists
  resources :items, only: [:destroy]
# Did the above to expose the /items/:id route to pass from the button_to POST action so the appropriate ITEM could be removed. 

```

Still slight bugs to work through…So while I didn’t find an answer to my question, I found a way.

---

<div class="post-metadata">

**Author:** ![phanhaiquang](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/phanhaiquang/32/2261_2.png) [@phanhaiquang](https://discuss.hotwired.dev/u/phanhaiquang)\
**Post date:** [October 8, 2021, 9:22am UTC](https://discuss.hotwired.dev/t/how-to-delete-turbo-stream-from-another-models-show-page/3330/3 "2021-10-08T09:22:06Z")

</div>

Hi Cabrace,

I am using below way for my project

For Stream  
\<%= turbo\_stream\_from “ **items** ” %\>  
\<%= turbo\_frame\_tag “ **items** ” do %\>  
\<%= render @items %\>  
\<% end %\>

For Frame (file \_item.html.erb)  
\<%= turbo\_frame\_tag dom\_id(item) do %\>  
…  
\<% end %\>

Thanks,
