【问题标题】:Adding threading and email subscriptions to comments [closed]为评论添加线程和电子邮件订阅[关闭]
【发布时间】:2014-02-23 01:36:39
【问题描述】:

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

我已经尝试了所有的评论宝石,但它们都有一些不足。

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

用户可以对帖子发表评论。每条评论都是可投票的(使用acts_as_votable gem)。用户得分由他们在 cmets 上获得的总票数构成。

这是我的 cmets 架构中的内容:

 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

在我的帖子模型中:

class Post < ActiveRecord::Base
  has_many :comments
end

在我的评论模型中:

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

在我的 cmets 控制器中:

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

在我看来:

<% @post.comments.each do |comment| %>
  <%= comment.body  %>

  <% if user_signed_in? && (current_user != comment.user) && !(current_user.voted_for? comment) %>

<%= link_to “up vote”, like_post_comment_path(@post, comment), method: :put %>

<%= comment.votes.size %>

<% else %>

<%= comment.votes.size  %></a>

<% end %>
<% end %>


<br />


<%= form_for([@post, @post.comments.build]) do |f| %>

  <p><%= f.text_area :body, :cols => "80", :rows => "10" %></p>

  <p><%= f.submit “comment” %></p>

<% end %>

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

<%= (@user.comments.map{|c| c.votes.count}.inject(:+))%>

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

如何使线程化 cmets 可投票?(父母和孩子)必须对路线做什么?

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

如何拉取用户对 cme​​ts 的所有投票计算的用户分数,包括子 cmets?

【问题讨论】:

  • 请保持你的问题简洁,只问一件事。

标签: ruby-on-rails ruby


【解决方案1】:

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

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

现在您的 cmets 有了一个树形结构。这允许 cmets 的 cmets 的 cmets 等等。

if my_comment.parent.nil? 那么你就是根。 if my_comment.children.empty? 那么评论中没有cmets。

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

【讨论】:

    【解决方案2】:

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

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

    您发现现有宝石的“不足之处”是什么? (因为acts_as_commentable 支持这个如果盒子)

    【讨论】:

    • 如何将acts_as_commentable 与我目前拥有的东西联系起来?
    • 是否有关于实现如何作为可注释线程的教程?我找不到如何为 cmets 添加回复链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2017-10-16
    • 1970-01-01
    • 2013-03-11
    • 2019-10-03
    相关资源
    最近更新 更多