0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails7】Turbo Frame / Turbo Stream の差し替えの仕組み

0
Last updated at Posted at 2025-12-14

はじめに

Rails7 の Turbo(Turbo Frame / Turbo Stream)についての学習をすすめるなかで、
解説記事ではコード量が多く、処理の流れが掴みにくいと感じました。

そこで本記事では、

Turbo Frame だけを使った場合

同じ処理を Turbo Stream で行った場合

フォーム送信後にリストを更新する流れ

を、最小構成のコードで整理します。

Step1:Turbo Frame だけを使った場合

やりたいこと:

index 画面の 「New To-Do」 をクリック

画面にすでに存在する
<%= turbo_frame_tag "new_todo_form" do %>の中身を

new.html.erb にある <%= turbo_frame_tag "new_todo_form" do %> の中身の内容で置き換える

index.html.erb
<%= link_to 'New To-Do',
            new_todo_path,
            data: { turbo_frame: "new_todo_form" },
            class: "button" %>

<%= turbo_frame_tag "new_todo_form" do %>
  <!-- 古い素材 -->
<% end %>

new アクション

def new
  @todo = Todo.new
end
new.html.erb(新しい素材)
<%= turbo_frame_tag "new_todo_form" do %>
  <%= render partial: "form", locals: { todo: @todo } %>
<% end %>

ポイント:

差し替えの条件は <%= turbo_frame_tag "new_todo_form" do %>の "new_todo_form"
部分が一致していること

正確には ERB <%= turbo_frame_tag "new_todo_form" do %> は HTML に変換されて `` のようになり
Turbo Frame は レスポンス内(newアクション→new.html.erb)の同じ id を持つ turbo-frame を探して置き換えます

Step2:同じ処理を Turbo Stream で行う場合

やりたいこと:

Step1 と同じ結果

ただし HTML(新しい素材)ではなく、指示書(Turbo Stream)で置き換える

index.html.erb(同じ)
<%= link_to 'New To-Do',
            new_todo_path,
            data: { turbo_frame: "new_todo_form" },
            class: "button" %>

<%= turbo_frame_tag "new_todo_form" do %>
  <!-- 古い素材 -->
<% end %>

new アクション

def new
  @todo = Todo.new

  respond_to do |format|
    # Turbo が無効な場合(通常の画面遷移)
    format.html

    # Turbo が有効な場合(指示書を返す)
    format.turbo_stream do
      render turbo_stream: turbo_stream.replace(
        "new_todo_form",
        partial: "form",
        locals: { todo: @todo }
      )
    end
  end
end

ポイント:

Turbo Stream が存在すると、HTML より優先される

replace("new_todo_form") が「どこを変えるか」を決めている

data:{turbo_frame} は「どのフレーム宛のリクエストか」を決めているだけ

Step3:フォーム送信後にリストを更新する

やりたいこと

フォームを submit

新しい To-Do を todos フレームに追加

フォームは 空欄にリセット

index.html.erb
<%= link_to 'New To-Do',
            new_todo_path,
            data: { turbo_frame: "new_todo_form" },
            class: "button" %>

<%= turbo_frame_tag "todos" do %>
  <!-- 古い素材 -->
<% end %>

<%= turbo_frame_tag "new_todo_form" do %>
<% end %>
_form.html.erb
<%= form_with model: todo, data: { turbo_frame: "todos" } do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title %>

  <%= f.label :completed %>
  <%= f.check_box :completed %>

  <%= f.submit %>
<% end %>

create アクション

def create
  @todo = Todo.new(todo_params)

  if @todo.save
    respond_to do |format|
      format.html { redirect_to todos_path }

      format.turbo_stream do
        render turbo_stream: [
          # todos に新しい todo を追加
          turbo_stream.append(
            "todos",
            partial: "todo",
            locals: { todo: @todo }
          ),

          # フォームを空欄に戻す
          turbo_stream.replace(
            "new_todo_form",
            partial: "form",
            locals: { todo: Todo.new }
          )
        ]
      end
    end
  end
end
_todo.html.erb
<%= turbo_frame_tag "todo_#{todo.id}" do %>
  <p>
    <%= todo.title %>
    <%= todo.completed ? "(Completed)" : "(Pending)" %>
  </p>

  <%= link_to 'Edit',
              edit_todo_path(todo),
              data: { turbo_frame: "todo_#{todo.id}" },
              class: "button" %>
<% end %>

新しい素材にも古い素材にも同じ turbo_frame_tag を書くのに、どうやって見分けているのか

画面にすでに存在している turbo-frame が「差し替えられる側(古い素材)」

Turbo は以下の順で動きます:

画面上の HTML を見る

id が一致する turbo-frame を探す

それをレスポンス内容で置き換える

結論:どちらが古いか/新しいかは「画面にあるかどうか」だけで決まる

補足: 今回はcontroller内にTurbo Streamの処理を書きましたが、

例えばnewアクションないのTurbo Streamの処理を以下のように書くこともできます

new.turbo_stream.erb
<%= turbo_stream.replace "new_todo_form" do %>
  <%= render "form", todo: @todo %>
<% end %>

newアクション

respond_to do |format|
    format.html
    format.turbo_stream
  end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?