首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多态注释的线程化

多态注释的线程化
EN

Stack Overflow用户
提问于 2014-02-28 13:11:02
回答 2查看 386关注 0票数 1

我已经建立了两个模型,它们可以通过相同的注释表进行注释:

我的评论模式:

代码语言:javascript
复制
  create_table "comments", force: true do |t|
    t.text     "body"
    t.integer  "commentable_id"
    t.string   "commentable_type"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

我的评论模式:

代码语言:javascript
复制
class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
  belongs_to :user
  acts_as_votable
end

我的电影模型

代码语言:javascript
复制
class Movie < ActiveRecord::Base
  belongs_to :user
  has_many :comments, as: :commentable
end

我的书模型:

代码语言:javascript
复制
class Book < ActiveRecord::Base
  belongs_to :user
  has_many :comments, as: :commentable
end

我的评论负责人:

代码语言:javascript
复制
def index
  @commentable = find_commentable
  @comments = @commentable.comments
end

def create
  @commentable = find_commentable
  @comment = @commentable.comments.build(params[:comment])
  @comment.user = current_user
  if @comment.save
    flash[:notice] = "Successfully created comment."
    redirect_to @commentable
  else
    render :action => 'new'
  end
end

  def upvote_movie
  @movie = Movie.find(params[:movie_id])
  @comment = @movie.comments.find(params[:id])
  @comment.liked_by current_user

  respond_to do |format|
    format.html {redirect_to :back}
  end
end


  def upvote_book
  @book = Book.find(params[:book_id])
  @comment = @book.comments.find(params[:id])
  @comment.liked_by current_user

  respond_to do |format|
    format.html {redirect_to :back}
  end
end


private

def find_commentable
  params[:commentable_type].constantize.find(params[:commentable_id])
end
end

如何将线程(回复评论)添加到我已经拥有的内容中?

下面是一个关于线程的博客:http://www.davychiu.com/blog/threaded-comments-in-ruby-on-rails.html

我只是不知道怎么把这两者结合起来。

以下是我在电影放映中看到的内容:

代码语言:javascript
复制
<%= render partial: "comments/form", locals: { commentable: @movie } %>


<% @comments.each do |comment| %>

<hr>
  <p>
   <strong><%= link_to comment.user.username, user_path(comment.user), :class => "user" %>
</strong> <a><%= "(#{time_ago_in_words(comment.created_at)} ago)" %></a>
  </p>

  <p>

    <%= simple_format(auto_link(comment.body, :html => { :target => '_blank' } )) %>
<% end %>

下面是我的评论表格:

代码语言:javascript
复制
<%= form_for [commentable, Comment.new] do |f| %>
  <%= hidden_field_tag :commentable_type, commentable.class.to_s %>
  <%= hidden_field_tag :commentable_id, commentable.id %>
  <p>
    <%= f.text_area :body %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-28 13:44:52

我浏览了你提到的那篇文章,发现那里的解决方案非常有限。

本文的基本思想是将评论本身设置为可评论。因此,嵌套注释实际上不是帖子的注释,而是父注释的注释。

缺点是明显和不可接受的:

  1. 很难把其他事情做好。例如,posts.comments.size不再是正确的。
  2. 您将很难依赖此结构。如果有一天您不想在线程中显示评论,而只是简单地显示,那么you...will就会大发雷霆。
  3. 如果你想在当前的评论系统上做这件事,那是很难的。

实际上,一个简单的解决方案可以解决这个问题:

  1. 在注释模型中添加一个额外的字段reply_to,引用其他注释的id。
  2. 添加注释时,如果reply_to id回复了一个注释,则添加它。
  3. 显示时,显示带有reply_to null的所有注释的列表。
  4. 然后,对于每个注释,显示嵌套注释有它的id。然后递归地做。
  5. 如果您想限制嵌套级别,可以添加一个额外的nested_level字段,从前端进入。如果嵌套限制为3,则不允许评论回复nest级别为3的注释。

添加:演示助手,用于递归呈现

代码语言:javascript
复制
def render_replied_comments(comment)
  if comment.has_reply
    comments.replies.each do |reply|
      render partial: 'comment', locals: {comment: reply}
      render_replied_comment(reply)
    end
  end
end

# View
@post.top_level_comments.each do |comment|
  render partial: 'comment', locals: {comment: comment}
end
票数 1
EN

Stack Overflow用户

发布于 2014-02-28 13:43:52

您将向注释模型添加一个parent_id,该模型是一个自引用关系。因此父注释的parent_id为零,所有子注释都有父注释的parent_id。你实际上是在建造一棵树。

祖先宝石是理想的或滚动自己的,良好的学习经验。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22096134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档