rails 评论/回复插件 acts_as_commentable_with_threading

acts_as_commentable_with_threading  ||   https://github.com/elight/acts_as_commentable_with_threading

 

# 在被评论的model里加

class Article < ActiveRecord::Base

  acts_as_commentable

end

 

# 添加评论, 其中build_from是model里构建new的方法

@article = Article.find(params[:id])

@user_who_commented = @current_user

@comment = Comment.build_from( @article, @user_who_commented.id, "Hey guys this is my comment!" )

 

# 添加评论的回复comment: 保存了的评论, 然后用这个方法移动到parent_comment的评论下,也就是回复, 回复的评论不能是自己,回复的commentable还是被评论的对象,只是把它移动到指定评论的下面。

comment.move_to_child_of(parent_comment)

 

# 取说有的评论及回复

@all_comments = @article.comment_threads

 

# 取第一级评论,对 article 的评论

@root_comments = @article.root_comments

 

判断一个评论是否有回复

@comment.has_children?

 

# 取一个评论的所以回复数量

@comment.children.size

 

# 取一个评论的所以回复按lft排序,正序{:parent_id=> :id}

@comment.children

 

# 取被评论对象

 @comment.commentable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值