为评论添加线程和电子邮件订阅

我已经构建了一个简单的rails应用程序,其中包含三个模型,post,用户和评论。

我已经尝试了每一个评论gem,他们都有一些不足。

所以我建立了自己的评论系统。

用户可以对post发表评论。 每条评论都是可投票的(使用acts_as_votable gem)。 用户得分由他们在评论中收到的总票数构成。

以下是我的架构中的评论内容:

create_table "comments", force: true do |t| t.text "body" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" t.integer "post_id" end 

在我的用户模型中:

 class User < ActiveRecord::Base has_many :comments acts_as_voter end 

在我的post模型中:

 class Post < ActiveRecord::Base has_many :comments end 

在我的评论模型中:

 class Comment < ActiveRecord::Base belongs_to :post belongs_to :user acts_as_votable end 

在我的评论控制器中:

 class CommentsController < ApplicationController def create post.comments.create(new_comment_params) do |comment| comment.user = current_user end respond_to do |format| format.html {redirect_to post_path(post)} end end def upvote @post = Post.find(params[:post_id]) @comment = @post.comments.find(params[:id]) @comment.liked_by current_user respond_to do |format| format.html {redirect_to @post} end end private def new_comment_params params.require(:comment).permit(:body) end def post @post = Post.find(params[:post_id]) end end 

在我的路线文件中:

 resources :posts do resources :comments do member do put "like", to: "comments#upvote" end end end 

在我看来:

          

"80", :rows => "10" %>

在用户个人资料视图中:(显示用户评分

  

我如何实现线程?(在一个层面上,我假设多个级别只是让它变得非常混乱)

如何使线程评论可以投票?(父母和子女)必须对路线做什么?

如何通过用户可以订阅的简单电子邮件通知来接收一条简短的消息,说明新评论已发布到他们的post中?

如何根据用户提出的评论(包括儿童评论)获得的所有投票计算用户得分?

如果我正确理解了这个问题,您希望允许对评论进行评论。 在这种情况下,在Comment模型中,您将需要parent_id:integer属性。 然后添加以下关联:

 class Comment ... belongs_to :parent, class_name: 'Comment' has_many :children, class_name: 'Comment', foreign_key: 'parent_id' ... end 

现在您有了一个树状结构供您评论。 这允许评论评论的评论等。

if my_comment.parent.nil? 那么你就是根本。 if my_comment.children.empty? 然后对评论没有评论。

树木的移动成本很高,因此添加最大深度可能很聪明。

你如何实现线程? (回答你的一个问题)

使注释到用户关联具有多态性,然后您可以以相同的方式添加注释到注释关联。

您发现现有gem的“不足”是什么导致您无法做到这一点? (因为如果盒子,acts_as_commentable支持这个)