2014-02-01 5 views
0

Итак, я работаю с красноватым кормом. Пользователи могут комментировать комментарии. Когда страница загружается, все комментарии скрыты. И когда они нажимают кнопку show, я хочу, чтобы они увидели все комментарии к этому сообщению.Показать вложенные комментарии по клику, в Rails

Текущая проблема: когда они нажимают на комментарии, все комментарии появляются для каждого сообщения, а не только для того, на который они нажали.

код, который генерирует список в posts_helper.rb

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 

Jquery код index.html.erb

<%= new_print_tree @posts %> 

    <script> 

    $(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(); 
     }) 
    }) 

</script> 

ответ

0

Использование:

$(this).closest('.nested_comments').show() # or hide() 

вместо

$(".nested_comments").show(); 
+0

Добавление «.closest()» просто спрятало кнопку и прекратило показывать комментарии. – marlonjs

Смежные вопросы