Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not render turbo_stream with only last 10 messages for each room #13

Closed
viktorianer opened this issue Apr 25, 2021 · 1 comment
Closed

Comments

@viktorianer
Copy link

I'm just trying to render only the @messages = @room.messages.last(10), after a new message is created.
I tried 4 different ways, because I did not understand how to do it.

What I did is to render all messages after format.turbo_stream in messages_controller.rb, so in app/views/messages/create.turbo_stream.slim it looks like (I use Slim templates...):

= turbo_stream.replace :messages
  #messages
    == render partial: "messages/messages", locals: { messages: @messages }

And on sender side it works, and I see only the last 10 messages. But, because Message broadcasts_to :room, it will use append on the receiver side and each receiver see then 10 older messages and each appended one.

How can I use broadcasts_to :room or after_create_commit -> { broadcast_append_to room } respectively to broadcasts only last 10 messages for each room?

It should be possible to do this, right?

@viktorianer
Copy link
Author

I just found out, in order to show the last 10 messages on sender side I could do in MessagesController this:

In create action just remove format.turbo_stream (and if you wish remove app/views/messages/create.turbo_stream.erb too) and it is working.
Also, no need for app/assets/javascripts/controllers/form_controller.js and data in app/views/messages/_form.html.slim.

Thanks a lot to @Deanout and this tutorial on YT https://youtu.be/NtTfYfWAzw0?t=1416

But, still, it will use append on the receiver side.

@dhh dhh closed this as completed Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants