Hotwire Turbo Frame returning all records rather than single created record

Viewed 9

I am using Hotwire Turbo Frame to create new record. My understanding was that when we create new record using Turbo Frame then only that single record will be replaced in index page rather than fetching all records from server. But when I look into Network tab, it returns all records from database. Only thing that it doesn't return is <head></head> content as expected. If it will return all record from database then it's not going to be fast. I most be doing something wrong. Below are my code snippets.

Controller

class QuotesController < ApplicationController
  def index
    @quotes = Quote.all
  end

  def new
    @quote = Quote.new
  end

  def create
    @quote = Quote.new(quote_params)
    if @quote.save
      respond_to do |format|
        format.html { redirect_to root_path, notice: 'Success' }
        format.turbo_stream
      end
    else
      render :new, status: :unprocessable_entity
    end
  end
end
(more code here ....)

View (index.html.erb)

 <h1>Quotes</h1>

 <%= link_to 'New Quote', new_quote_path, data: { turbo_frame: dom_id(Quote.new) } %><br>

 <%= turbo_frame_tag Quote.new %>

 <%= turbo_frame_tag 'quotes' do %>
   <%= render @quotes %>
 <% end %>

View (new.html.erb)

 <h1>New Quote</h1>

 <%= turbo_frame_tag @quote do %>
   <%= render "form" %>
 <% end %>

create.turbo_stream.erb

 <%= turbo_stream.prepend 'quotes', @quote %>
 <%= turbo_stream.update Quote.new, '' %>
0 Answers
Related