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.
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.
@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.
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)
- Mixed Content Error with Turbo Drive
- OT: Attackers might be trying to steal your information from discuss.hotwire.dev HOT 2
- Turbo refresh can hijack user navigation HOT 3
- import "@hotwired/turbo-rails" errors HOT 1
- Using data-turbo-confirm on plain (non-form) buttons HOT 3
- Adding stimulus breaks a test HOT 1
- how do you install turboframes into a rails 5 project that migrated to rails 7 using only sprockets (skipping all the webpack & 6 bs)? HOT 2
- What are the controls effected before injecting `text/vnd.turbo-stream.html`?
- turbo_stream.remove_all Not generating proper selector for targets.
- idiomorph merges attributes on `<turbo-cable-stream-source>` and `connectedCallback()` doesn't run HOT 2
- Add morph to StreamActions HOT 5
- how do I batch multiple turbo stream broadcasts?
- <turbo-cable-stream-source> tag can affect visual layout HOT 1
- broadcast_update_to partial should be smarter and extract the inner_html, it feels like a bug
- Turbo not loading on Safari in production HOT 1
- Any Security Constraints to turbo_method: 'post' functionality?
- using the view transitions API? HOT 1
- Unable to render layouts when using inline turbo stream responses
- 8.0.5: cdn (and thus importmap pins) has broken import paths "CbGBsmqA.js" instead of cable.js
- Turbo Frame + Nginx Cache + data-action-advance
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.