-
Notifications
You must be signed in to change notification settings - Fork 305
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
Syntax for returning multiple turbo streams #77
Comments
While this is possible, it’s discouraged. I’d rather we don’t promote this. You can of course always do whatever you want, but in my style guide, this is much worse than using a template. |
@dhh One example where this could be useful is when one has a paginated table of items. And lets say you delete one item. Now if you go to the next page, you'll miss one item, because the count is off by one - that one item is now on the previous page. With multiple streams you could delete the desired item, and append one item that now belongs to this page. |
I'd like to point out that - unless I'm completely mistaken - @dhh's comment refers to multiple page updates from the controller as opposed to using a |
I do follow DHH's recommendations both with ruby and php but for this one, come on DHH, cant see no harm with this. Without this, i'd have to refresh the entire page. |
What @jeanmartin said. By all means include multiple commands in your turbo stream response! It was designed for this. What I don't like is doing that inline in the controller rather than in a template. Just like rendering inline ERB templates in a controller file is discouraged. |
anyone know the syntax so that I could avoid write this ?
I want to change this |
what we did - we use view components - but you can do same approach with partials .... add these helpers in our app controller (or app helper or ...)
and then you can use it like this
|
A couple years later and I came across this thread. In the documentation/codebase I found
@dhh , have things changed? Or is creating multiple action.turbo_stream.erb files still the preferred method? |
I think this syntax ( I'm still digging, but so far, this returns a content length mismatch. When changed to a template or Phlex component, tests pass.
|
I think I just stumbled on your issue @davidalejandroaguilar.
Would give me a content length mismatch when testing with selenium-webdriver adding a
Hope that helps you or anyone else having the same issue :D |
Following up on DHH’s comment about using turbo-stream templates rather than inline rendering, it’s pretty easy to do: <%= turbo_stream.replace(:flash, partial: "layouts/flash", locals: { notice: "Message posted!" }) %>
<%= turbo_stream.append(:messages, partial: "messages/message", locals: { message: @message }) %> Note the change from I’m new to turbo streams but I find turbo-stream templates more convenient and more Rails Way™ than inline turbo streams. It’s also easier to access helper methods from a template. |
Through trial and error I realised that a controller action can return multiple turbo streams. An example would be if you want to append an item to a list, and also show an alert that the item was created.
The syntax is a bit funky if you do it through a controller:
(you could also do it through a
.turbo_stream.erb
file, but I like having this in the controller)Questions:
The text was updated successfully, but these errors were encountered: