The hotwire-rails was just released today by Basecamp. It's a collection of JavaScript & Ruby libaries which allows for making some pretty rich user experiences.
The big thing I like, was how easy it is to create real-time partials in Ruby on Rails.
Now we just the need code:
# app/models/post.rb
class Post < ApplicationRecord
# We're going to publish to the stream :posts
# It'll update the element with the ID of the dom_id for this object,
# Or append our posts/_post partial, to the #posts <tbody>
broadcasts_to ->(post) { :posts }
end
Then within the views, having:
<!-- app/views/posts/index.html.erb -->
<%= turbo_stream_from :posts %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody id="posts">
<%= render @posts %>
</tbody>
</table>
<!-- app/views/posts/_post.html.erb -->
<tr id="<%= dom_id post %>">
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
In a manner of speaking I assume that the
turbo_stream_from
subscribes to the PostsApplicationRecord
'sbroadcast_to
?Additionally is
@posts
available to the view because of the broadcast, or are you just not showing your method implementations?