Skip to content

Commit 7c1c53c

Browse files
committed
Live updates with Turbo for Items
1 parent 7e4d403 commit 7c1c53c

File tree

7 files changed

+66
-347
lines changed

7 files changed

+66
-347
lines changed

.idea/baka-home.iml

+46-340
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gem "stimulus-rails"
2828
gem "jbuilder"
2929

3030
# Use Redis adapter to run Action Cable in production
31-
# gem "redis", "~> 4.0"
31+
gem "redis", "~> 4.0"
3232

3333
# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
3434
# gem "kredis"

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ GEM
168168
thor (~> 1.0)
169169
zeitwerk (~> 2.5)
170170
rake (13.0.6)
171+
redis (4.5.1)
171172
regexp_parser (2.2.0)
172173
reline (0.3.1)
173174
io-console (~> 0.5)
@@ -222,6 +223,7 @@ DEPENDENCIES
222223
jbuilder
223224
puma (~> 5.0)
224225
rails (~> 7.0.1)
226+
redis (~> 4.0)
225227
selenium-webdriver
226228
sprockets-rails
227229
sqlite3 (~> 1.4)

app/models/item.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class Item < ApplicationRecord
22
belongs_to :location
33
belongs_to :brand
4+
5+
after_create_commit { broadcast_append_to 'item' }
6+
after_update_commit { broadcast_replace_to 'item' }
7+
after_destroy_commit { broadcast_remove_to 'item' }
48
end

app/views/items/index.html.erb

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
<%= turbo_stream_from "item" %>
12
<p style="color: green"><%= notice %></p>
23

34
<h1>Items</h1>
45

56
<div id="items">
67
<% @items.each do |item| %>
7-
<%= render item %>
8-
<p>
9-
<%= link_to "Show this item", item %>
10-
</p>
8+
<%= turbo_frame_tag "item" do %>
9+
<%= render item %>
10+
<p>
11+
<%= link_to "Show this item", item, 'data-turbo-frame': :_top %>
12+
</p>
13+
<% end %>
1114
<% end %>
1215
</div>
1316

app/views/items/show.html.erb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<p style="color: green"><%= notice %></p>
22

3-
<%= render @item %>
3+
<%= turbo_stream_from "item" %>
4+
<%= turbo_frame_tag "item" do %>
5+
<%= render @item %>
6+
<% end %>
47

58
<div>
69
<%= link_to "Edit this item", edit_item_path(@item) %> |

config/cable.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
development:
2-
adapter: async
2+
adapter: redis
3+
url: redis://localhost:6379/1
34

45
test:
56
adapter: test

0 commit comments

Comments
 (0)