title: Show nested comments on click, in Rails
body: <p>So I'm working on a reddit-like feed. Users can comment on comments. When the page loads, all the comments are hidden. And when they click show, I want them to see all the comments for that post.</p>
  
  <p>Current problem: When they click on show comments, all the comments appear for every post, not just the one they clicked on.</p>
  
  <p>Code that generates the list in posts_helper.rb</p>
  
  <pre><code>def new_print_tree(posts)
    content_tag(:ul) do
      posts.map do |post|
          if post.parent.nil?
            content_tag(:li) do
              content_tag(:div, post.post) do
                  content_tag(:div) do
  link_to(post.user.username, user_path(post.user), class: "username user" + post.user.id.to_s) + follow_button(post.user) + content_tag(:p, post.post, class: "root" ) + comment_box(post) + show_comment_button + view_comments(post)
                      end #content_tag :div do
              end + #content_tag :div, post.post
              new_print_tree(post.children)
          end #content_tag :li
  else
            content_tag(:li, class: "nested_comments") do
              content_tag(:div, post.post) do
                  content_tag(:div) do
                      link_to(post.user.username, user_path(post.user), class: "username user" + post.user.id.to_s) + follow_button(post.user) + content_tag(:p, post.post, class: "child" ) + comment_box(post) + show_comment_button + view_comments(post)
                      end #content_tag :div do
              end + #content_tag :div, post.post
              new_print_tree(post.children)
          end #content_tag :li            
  
        end
      end.join.html_safe #post.map
    end #content_tag ul,class
  end #def 
  </code></pre>
  
  <p>Jquery code in index.html.erb</p>
  
  <pre><code>&lt;%= new_print_tree @posts %&gt;
  
      &lt;script&gt;
  
      $(document).on("page:change", function(){
          $(".comment_form").hide();
          $(".nested_comments").hide();
  
          $(".show_comment").click(function(){
              $(this).prev(".comment_form").show();
                  $(this).hide();
          })
  
          $(".view_comment_button").click(function(){
              $(this).hide();
              $(".nested_comments").show();
          })
      })
  
  &lt;/script&gt;
  </code></pre>