turbo stream from controller
Controller doing all the workÂ
def create # ... respond_to do |format| if @entry.save format.turbo_stream do render turbo_stream: turbo_stream.before( "id-of-html-element", partial: "entries/entry", locals: { entry: @entry }) end format.html { redirect_to @entry, notice: "Successfully created." } format.json { render :show, status: :created, location: @entry } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @entry.errors, status: :unprocessable_entity } end end end
Controller action doing the work
def create # ... respond_to do |format| if @entry.save format.turbo_stream format.html { redirect_to @entry, notice: "Successfully created." } format.json { render :show, status: :created, location: @entry } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @entry.errors, status: :unprocessable_entity } end end end
<%# app/views/entries/create.turbo_stream.erb %> <%= turbo_stream.before "id-of-html-element" do %> <%= render partial: @entry.to_partial_path, locals: { entry: @entry } %> <% end %>
or:
<%# app/views/entries/create.turbo_stream.erb %> <%= turbo_stream.before "id-of-html-element" do %> <%= render @entry %> <% end %>
Rails translates @entry to @entry.to_partial_path which equals to "entries/entry" in this case