1

我使用 ActionText / Trix Editor 创建的二级评论未显示在我的 rails 应用程序中。原始帖子显示良好,一级评论也是如此。但是,评论的评论不会显示。

我使用 Rails 6 创建了一个包含帖子和嵌套评论的应用程序。帖子和评论正在数据库方面工作。但是,在我安装 ActionText 之后,我可以允许用户使用富文本发布和评论,只有发布和第一级评论显示他们各自的正文内容。

帖子和第一级评论将 post.body 和 comment.body 呈现在“trix-content”div 中。但是,第二级嵌套评论的输出似乎并没有抓住comment.body。

Comment Model Code:

class Comment < ApplicationRecord
  has_rich_text :body
  belongs_to :user
  belongs_to :commentable, polymorphic: true
  belongs_to :parent, optional: true, class_name: "Comment"


  def comments
    Comment.where(commentable: commentable, parent_id: id)
  end
end

Comment Controller Code:

class CommentsController < ApplicationController
  before_action :authenticate_user!

  def create
    @comment = @commentable.comments.new(comment_params)
    @comment.user = current_user
    if @comment.save
      redirect_to @commentable, notice: "Comment was successfully created."
    else
      redirect_to @commentable, alert: "Something went wrong"
    end
  end

  def destroy
    @comment = @commentable.comments.find(params[:id])
    @comment.destroy
    redirect_to @commentable
  end

  private
  def comment_params
    params.require(:comment).permit(:body, :parent_id)
  end

end

Comment Form Code:

<%= form_with model: [commentable, Comment.new], local: true, html: { class: local_assigns[:class], data: { target: local_assigns[:target]}} do |form| %>

<div class="form-group">
  <%= form.rich_text_area :body, placeholder: "Add a comment", class: "form-control" %>
</div>

<div class="form-group">
  <%= form.hidden_field :parent_id, value: local_assigns[:parent_id] %>
  <%= form.submit class: "btn btn-primary" %>
</div>

<% end %>


Comment View/Show Page Code:

<% nesting = local_assigns.fetch(:nesting, 1) %>
<% max_nesting = local_assigns[:max_nesting] %>

<div <div class="col-lg-3">

</div>

<div class="col-lg-7">
  <ul class="media-list media-list-stream mb-2">
    <li class="media list-group-item mb-2">

  <img class="mr-0">
    <%= image_tag user_avatar(comment.user, 60), class: "rounded-circle" %>
  </img>

<div class="media-body">
  <div class="media-heading">
    <small class="float-right text-muted"> <%= comment.created_at.strftime("%b %e, %Y") %> </small>
  <h6 class="ml-3"><%= comment.user.name %></h6>
  </div>
  <div class="media ml-3">
    <p><%= comment.body %></p>
  </div>
  <div class="media-footer ml-3">




    <div data-controller="reply">
    <small>
      <%= link_to "Reply", "#", data: { action: "click->reply#toggle" } %>
      <%= link_to "Delete", [comment.commentable, comment], method: :delete, data: {confirm: "Are you sure?"} if comment.user == current_user%>
    </small>

    <%= render partial: "comments/form", locals: {
      commentable: comment.commentable,
      parent_id: reply_to_comment_id(comment, nesting, max_nesting),
      class: "d-none",
      target: "reply.form"
    } %>

    </div>
    </div>
    </div>
</li>
    <%= render comment.comments, nesting: nesting + 1, max_nesting: local_assigns[:max_nesting] %>
  </ul>
  </div>

我怀疑这个问题与评论评论的关联有关,因为当我删除嵌套并评论评论时,我看到了相同的结果。好像有人发表了一条空白评论。

4

1 回答 1

4

所以,来自 GoRails 的 Chris Oliver 帮助我解决了这个问题。我必须为每个 Trix Editor 表单添加一个输入 ID。基本上,Rails 没有正确识别二级评论表单。

我所要做的就是这样的:

<%= form.rich_text_area :body, placeholder: "Add a comment", class: "form-control", id: form.object.object_id %>
于 2019-08-14T04:39:33.723 回答