Marc Jaramillo

Joined

1,820 Experience
10 Lessons Completed
1 Question Solved

Activity

Hi Chris,

I was just wondering if there is any possibility of getting a video or some tips on including Devise current_user data or user roles inside a turbo stream - specifically to show/hide edit and delete links? I've gotten a threaded comment feature working after going through the Real-Time Nested Comments videos, but I can't get the edit/delete links working. In this post, DHH says "Partials used for turbo streaming have to be free of global references, as they’re rendered by the ApplicationRenderer, not within the context of a specific request" and in this closed issue he says "You'll need to make a partial that's not dependent on global state like this to work. You can use client-side show/hide using a stimulus controller instead". I've tried to find ideas on work arounds, and I've seen a few workarounds: this one with CSS and this one using local variables. I've tried adapting both of these approaches to my project, but no dice.

Posted in Realtime Nested Comments: Part 3 Discussion

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 nesting and 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.

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 nesting and 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.

I asked this question in the discussion on the tutorial itself, but there doesn’t seem to be any activity there. I went through the nested comments series, and the last three videos focused on real-time changes using Hotwire and Turbo. In the first part of the series, Chris introduced a way to limit how deeply comments could be nested, but he doesn’t show this feature in the Hotwire version. I'm curious how the nesting depth limit that was implemented with the original version can be adapted for a Hotwire/Turbo app? The two versions seem drastically different from each other, so I’m not sure where to start. I’m still new to Hotwire and Turbo so any guidance would be greatly appreciated.

Posted in Realtime Nested Comments: Part 3 Discussion

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.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.