Hotwire Discussion

How to call the same turbio_stream template from different controller actions

I am really struggling to understand turbo_streams. For some reason my turbo_frames aren’t working in the way I was expecting as it seems to be replacing the whole page (according to Chrome Developer screens). I thought it replaced only the section on the page within the frame.

The problem I now seem to have … I have three controller methods which either delete, add or completely replace a list of time codes for a video I’m processing.

Each turbo_stream.erb file calls exactly the same code as shown:

<%= turbo_stream.replace "updatetimecode" do %>
  <%= turbo_frame_tag "updatetimecode" do %>
    <%= render partial: 'showtimecodes' %>
  <% end %>
<% end %>

It seems really silly that I have three files with exactly the same thing:

addtimecode.turbo_stream.erb, add_timecodes.turbo_stream.erb and delete_timecodes.turbo_stream.erb which all correspond with controller actions.

One of which is below which I use following some advice on how to get turbo_stream to replace just the section of the page which turbo_frames seems to not be doing.

  def changetime
    # Designed to load the video at the time code
    time = params[:time]
    @newtime = time.split(':').map(&:to_i).inject(0) { |a, b| a * 60 + b }
    respond_to do |format|
      format.turbo_stream {}
    end
  end

How do I get each controller to replace the section of the page, but by calling only one turbo_stream template?

Thanks in advance for any help you can give.

PS. Is there anyone who has done a kick butt tutorial on how to make this work (from a relatively new person perspective)? I find the examples on the page hard to follow from a Rails point of view.

The approach I might take is redirect all other actions to a single new action (on the same controller or a new controller) which returns the turbo_stream. The redirect should carry the turbo_stream to the next action, so your logic should work.

I’ve done this before, but not with params. You may need to include the params in the redirect in order to get it to work properly.