Turbo streams that load data


So, i have a basic chat container and i have a button to load more messages from the chat if it exists.
Currently, i am trying to use turbo_stream.prepend to prepend to the chat container.

So, this is what the code looks like

class MessagesController < ApplicationController
  def index
    @messages = @conversation.messages.page(params[:page]).per(2)

and in the view messages/index.html.erb im trying to do something like this

<%= turbo_stream.prepend "conversation", class: "w-full flex flex-col" do %>
  <%= render @messages, business: @conversation.business %>
<% end %>

and the button to call the method is

  class="h-full flex flex-col justify-between px-4 overflow-x-hidden overflow-y-auto"
  id="<%= dom_id(conversation) %>">
    <%= link_to "get more", conversation_messages_path(conversation_id: conversation, params: { page: 2 }) %
  <% unless conversation.nil? %>
    <%= render conversation.messages, business: conversation.business %>
  <% end %>

However, when i click the button i end up with an html response, but when i add

respond_to |format| do

i get error saying

However, when i wrap the link tag with a turbo_frame_tag i get it working but feels hacky. Any ideas ?


From hotwired/turbo-rails:

Note that the helpers that turn link_to into remote invocations will not currently work with Turbo. Links that have been made remote will not stick within frames nor will they allow you to respond with turbo stream actions. The recommendation is to replace these links with styled button_to , so you’ll flow through a regular form, and you’ll be better off with a11y compliance.

1 Like