Realtime Nested Comments: Part 3 Discussion
Very cool. I imagine a real-time comment feed like this will make a website feel come alive.
One issue I’d love to see addressed in a follow up video is the page kinda moving around as you’re reading comments. I believe Disqus solves this by not I’m immediately appending new comments, but adding a placeholder like “5 new comments” which you can click to reveal.
I imagine we could do something similar with Stimulus but it right require some changes to the broadcasts.
Are there any performance concerns to keep in mind with having a turbo_stream_from tag for each comment? Does this create a separate connection for each comment? I'm new to websockets/redis, not even quite sure the best way to test this lol
Loving these episodes, and learning so much! Thank you!
I'm getting the error Devise::MissingWarden in Comments::Comments#create when trying to call the current_user helper in the _comment partial. To check if the current_user is the author of the comment.
How does this work with someones avatar thats uploaded using activestorage? I get the following url for the image
where the example.org was set in
application_controller_renderer.rb. Even after replacing it to "localhost" it still doesn't work. When you do a full page reload all the avatars show up.
Great series Chis thanks for putting this together, it was eye opening to how comments can really come alive with Hotwire. A bit of a newbie here, but would it be possible to take it a step further and bring nested comments inline akin to Google Docs as a way to bring more context into the comments themselves?
Great series! I'm curious how the nesting depth limit that was implemented with the AJAX version can be adapted for this approach? There's quite a bit that's different between the two versions, so I'm not sure where to even begin. Still new to Turbo and Hotwire, so any guidance would be appreciated.
If anyone is interested, I figured out what I needed to do. For context: in the first half of the Nested Comment Threads in Rails series, Chris introduces two local variables inside the
comments/_comment.html.erb partial called
max_nesting. He also creates a helper method called
reply_to_comment_id(comment, nesting, max_nesting) that sets the parent_id of the reply comment. I couldn't get this approach to work with Hotwire and Turbo, but thanks to a comment by Jay Killeen on Nested Comment Threads in Rails - Part 3 I was able to get this working:
Create a migration to add nesting to comments:
class AddNestingToComments < ActiveRecord::Migration[6.1] def change add_column :comments, :nesting, :integer end end
Add this inside the Comment model:
def set_nesting if self.parent.present? && self.parent.nesting.present? self.nesting = self.parent.nesting + 1 else self.nesting = 1 end end def self.max_nesting 2 end
Make this change inside the Commentable concern:
def create ... ... if @parent&.nesting.blank? || @parent&.nesting < Comment.max_nesting @comment.parent_id = @parent&.id @comment.nesting = @comment.set_nesting else @comment.parent_id = @parent&.parent_id @comment.nesting = @comment.set_nesting end ... ... end
This may not be the best way to handle things, so if anyone has another approach that is better please feel free to comment.