Skip to content

Conversation

@palkan
Copy link
Member

@palkan palkan commented Apr 8, 2021

Hotwire allows use to leverage HTML-over-the-Wire as much as possible and get rid of unnecessary JavaScript.

What's in a poke

  • Migrate items deletion and completion to Turbo Frames
  • Migrate real-time list updates to Turbo Streams with a bit of Stimulus (via stimulus-use)
  • Migrate chat to AJAX, Turbo Streams and custom elements

@palkan palkan marked this pull request as draft April 8, 2021 07:49
Comment on lines 28 to 29
flash.now[:notice] = "Item has been updated"
render partial: "update", locals: {item}, content_type: "text/vnd.turbo-stream.html"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 text/vnd.turbo-stream.html content type in order to activate stream elements confusion in your (great!) article. See the Turbo::StreamsHelper documentation for more.

Copy link
Member Author

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

Copy link

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already updated)

Copy link

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.

Copy link
Member Author

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: ....

end
end
flash.now[:notice] = "Item has been deleted"
render partial: "update", locals: {item}, content_type: "text/vnd.turbo-stream.html"
Copy link

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?

Copy link

@iguanus iguanus Apr 12, 2021

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?

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 }

Copy link

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/facepalm. You are right.

Copy link
Member Author

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤗

@palkan palkan force-pushed the master branch 13 times, most recently from 0f9a0b6 to f311544 Compare January 28, 2023 01:03
@palkan palkan force-pushed the master branch 2 times, most recently from 2e4efd0 to 7b2111f Compare April 2, 2024 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants