如何获取使用ActionText(Trix编辑器)创建的嵌套注释以显示在Rails项目中?

时间:2019-07-16 01:37:23

标签: ruby-on-rails trix actiontext

使用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>

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

1 个答案:

答案 0 :(得分:2)

因此,GoRails的Chris Oliver帮助我解决了这个问题。我必须为每个Trix编辑器表单添加一个输入ID。基本上,Rails不能正确识别第二级注释形式。

我要做的只是这样:

<%= form.rich_text_area :body, placeholder: "Add a comment", class: "form-control", id: form.object.object_id %>