I've created a small test app to explore hotwire and I'm having trouble after I click update, my edit button doesn't work any more. I have a jobs model and with-in the show I can click edit and the turbo frame will replace the contents with the edit form. I can then hit update and the show contents is updated without the page reload, but the edit button no longer works.
Job Show ERB
<p id="notice"><%= notice %></p>
<%= Time.now %>
<%= turbo_stream_from @job %>
<%= turbo_frame_tag dom_id(@job) do %>
<%= render "job_show", job: @job %>
<%= link_to 'Edit', edit_job_path(@job) %> |
<%= link_to 'Back', jobs_path, 'data-turbo-frame': :_top %>
<% end %>
Job show partial
<div id="<%= dom_id job %>" class="job">
<p>
<strong>Code:</strong>
<%= job.code %>
</p>
<p>
<strong>Name:</strong>
<%= job.name %>
</p>
</div>
Job controller
# PATCH/PUT /jobs/1 or /jobs/1.json
def update
respond_to do |format|
if @job.update(job_params)
format.html { redirect_to @job, notice: "Job was successfully updated." }
format.json { render :show, status: :ok, location: @job }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
Job model
class Job < ApplicationRecord
# We're going to publish to the stream :jobs
# It'll update the element with the ID of the dom_id for this object,
# Or append our jobs/_job partial, to the #jobs <tbody>
broadcasts_to -> (job) {:jobs}
end
I have noticed that when it's in the error state and I cannot hit the 'edit' button, if I use inspect in the browser can change the turbo_frame line it then works;
From
<turbo-frame id="job_7" src="http://localhost:3001/jobs/7">
To
<turbo-frame id="job_7">
What is putting the 'src' option onto that turbo-frame line? Can I disable that to get this working?
Full code: https://github.com/map7/hotwire_jobs_example
When you want to target a resource from outside the frame, remember to use the correct target, as in:
That will help you handle both
Edit
andBack
actions.You also need to tell your controller how to stream, by doing: