-
Notifications
You must be signed in to change notification settings - Fork 53
Hotwire #16
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
base: master
Are you sure you want to change the base?
Hotwire #16
Conversation
app/controllers/items_controller.rb
Outdated
| flash.now[:notice] = "Item has been updated" | ||
| render partial: "update", locals: {item}, content_type: "text/vnd.turbo-stream.html" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| flash.now[:notice] = "Item has been updated" | |
| render partial: "update", locals: {item}, content_type: "text/vnd.turbo-stream.html" | |
| respond_to do |format| | |
| format.turbo_stream do | |
| render partial: "update", locals: {item} | |
| end | |
| format.html do | |
| flash[:notice] = "Item has been updated" | |
| redirect_to workspace | |
| end | |
| end |
Untested! But I think this addresses the I found that Turbo expects HTTP response to have the
confusion in your (great!) article. See the text/vnd.turbo-stream.html content type in order to activate stream elementsTurbo::StreamsHelper documentation for more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrote this part a bit: 860c706
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet! Maybe update your blog post too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already updated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of this section in particular:

The preferred way to respond with Turbo Streams in Rails is with implicit .turbo_stream.erb templates (e.g. 860c706) and/or format.turbo_stream { … } (in respond_to) blocks. They set the custom response content type automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it work without respond_to? I mean: render turbo_stream: "update", locals: ....
app/controllers/items_controller.rb
Outdated
| end | ||
| end | ||
| flash.now[:notice] = "Item has been deleted" | ||
| render partial: "update", locals: {item}, content_type: "text/vnd.turbo-stream.html" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does locals: {item} expand to locals: {item: item} somehow or is this a JavaScript-brain typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
locals: {item}expand tolocals: {item: item}somehow or is this a JavaScript-brain typo?
That's right, @javan. In ES6, there's a shorthand version for properties with a value coming from a variable with the same name. So, having { item } will is the same as writing { item: item }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a Ruby file though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/facepalm. You are right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does locals: {item} expand to locals: {item: item} somehow
Yes, via Ruby Next 🙂
It's a proposed feature: https://bugs.ruby-lang.org/issues/15236
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤗
0f9a0b6 to
f311544
Compare
2e4efd0 to
7b2111f
Compare
Hotwire allows use to leverage HTML-over-the-Wire as much as possible and get rid of unnecessary JavaScript.
What's in a poke