Comments (11)
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.
from turbo-rails.
@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.
from turbo-rails.
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 .turbo_stream.erb
template. Of course, updating multiple sections in one response is reasonable, there are many valid use cases.
There seems to be quite a bit of confusion across the web linking to his comment here, claiming dhh is discouraging updating multiple sections.
from turbo-rails.
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.
from turbo-rails.
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 ...)
def turbo_stream_update(key, component)
@turbo_stream_actions ||= []
@turbo_stream_actions << turbo_stream.update(key, view_context.render(component))
end
# more if you want
def actions()
@turbo_stream_actions
end
and then you can use it like this
turbo_stream_update("someid", SomeComponent.new(param1: somevalue) )
turbo_stream_update("otherid", OtherComponent.new(other: othervalue) )
render turbo_stream: actions
from turbo-rails.
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.
from turbo-rails.
anyone know the syntax so that I could avoid write this ?
render turbo_stream: turbo_stream.prepend("flash", partial: "layouts/flash_messages"), status: :unprocessable_entity
I want to change this turbo_stream.prepend("flash", partial: "layouts/flash_messages")
with my *.turbo_stream.erb
files
from turbo-rails.
A couple years later and I came across this thread. In the documentation/codebase I found Module: Turbo::Streams::TurboStreamsTagBuilder
which had the following description:
Most turbo streams are rendered either asynchronously via Turbo::Broadcastable/Turbo::StreamsChannel or rendered in templates with the turbo_stream.erb extension. But it’s also possible to render updates inline in controllers, like so...
@dhh , have things changed? Or is creating multiple action.turbo_stream.erb files still the preferred method?
from turbo-rails.
I think this syntax (render turbo_stream: [...]
) is now broken on 7.1 in system tests (at least with selenium-webdriver
4.14.0).
I'm still digging, but so far, this returns a content length mismatch. When changed to a template or Phlex component, tests pass.
render turbo_stream: [...], content_type: "text/vnd.turbo-stream.html"
also doesn't work.
from turbo-rails.
I think I just stumbled on your issue @davidalejandroaguilar.
render turbo_stream: [
turbo_stream.prepend(:target_id, html),
turbo_stream.remove(:other_target_id)
]
Would give me a content length mismatch when testing with selenium-webdriver adding a .join
to array got it sorted.
render turbo_stream: [
turbo_stream.prepend(:target_id, html),
turbo_stream.remove(:other_target_id)
].join
Hope that helps you or anyone else having the same issue :D
from turbo-rails.
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 message
to @message
.
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.
from turbo-rails.
Related Issues (20)
- `refresh` action is missing from Turbo::Streams::TagBuilder HOT 5
- Turbo Frame Responses missing tracked elements HOT 6
- Usage of anchor HOT 5
- Websocket doesn't work on production
- rails tests fail after turbo-rails upgrade from 1.5.0 to 2.0.3 HOT 4
- Single quotes not properly handled in Turbo Streams broadcast
- Content duplication on using render in controllers HOT 1
- Chrome reloads assets in the Link preload header HOT 1
- Uglifier::Error during assets:precompile HOT 1
- Turbo not streaming changes properly / window.Turbo is undefined HOT 3
- InstaClick prefetch not working with fragment cache
- Prefetch causes turbo stream to render on hover instead of on click HOT 2
- Inverted parameters in stream responses HOT 2
- unexpected routes added as part of the turbo rails gem HOT 2
- When streaming from a worker, path helpers append a domain `https://example.com` HOT 5
- Turbo Documentation need a "Broadcast" chatper
- Refresh broadcasts generated without changes
- Incompatible with Solid_Queue HOT 1
- turbo_frame_request_id safe operator in not handled correctly HOT 2
- Shouldn't broadcast refresh if streamables == [nil] HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from turbo-rails.