Challenge 1: A Turbo Frame — Solution
<%= turbo_frame_tag "post_#{@post.id}" do %>
<%= @post.title %>
<%= link_to "Edit", edit_post_path(@post) %>
<% end %>
# app/views/posts/edit.html.erb must wrap its content in a frame with the
# SAME id for the swap to work:
<%= turbo_frame_tag "post_#{@post.id}" do %>
<%= form_with model: @post do |f| %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
<% end %>
=begin
Notes:
- "post_#{@post.id}" gives each post's frame a unique id -- if two different
posts' frames on the same page (e.g. an index listing several posts)
shared one id, Turbo could swap the wrong one.
- Clicking the Edit link still sends a completely normal GET request to
edit_post_path(@post) -- no special routing or controller code is needed
for Turbo Frames to work.
- The swap only happens because edit.html.erb wraps its form in a
turbo_frame_tag using the exact same id string. If the ids didn't match,
Turbo would fall back to a full page navigation instead of an in-place
swap.
- Nothing outside the frame (navigation, other posts, sidebar) re-renders
or even re-requests -- only the matched frame's contents are replaced.
=end