所以我正在开发类似reddit的Feed。用户可以评论评论。页面加载时,所有注释都被隐藏。当他们点击show时,我希望他们看到该帖子的所有评论。
当前问题:当他们点击显示评论时,每个帖子都会显示所有评论,而不仅仅是他们点击的评论。
在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
index.html.erb中的Jquery代码
<%= 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 :(得分:0)
使用:
$(this).closest('.nested_comments').show() # or hide()
而不是
$(".nested_comments").show();