【问题标题】:Rails: acts_as_commentable_with_threading children's comment form not workingRails:acts_as_commentable_with_threading 儿童评论表单不起作用
【发布时间】:2013-09-19 17:31:45
【问题描述】:

所以我将 acts_as_commentable_with_threading 用于类似于 Reddit 的评论系统。

所以在一个项目的显示页面上,我有一个 form_for,它有 (items/show.html.haml):

  - if user_signed_in?
    %h5 Have something to say?
    = form_for([@item, @new_comment], remote: true) do |f|
      .form-group
        = f.text_area :body, class: "form-control", rows: "3"
        = f.hidden_field :user_id, value: current_user.id
      .form-group
        = f.submit "Submit", class: "btn btn-sm"

使用控制器 (items_controller.rb):

  def show
    @item = Item.find(params[:id])
    @comments = @item.comment_threads
    if user_signed_in?
      @new_comment = Comment.build_from(@item, current_user.id, "")
    end
  end

然后会有一个 create.js.erb 将新创建的评论附加到页面上

$('#comments').append("<%= escape_javascript(render partial: 'comment', locals: { comment: @comment } ) %>");

if ("<%= @comment.body %>") {
    $("#comment_body").val('')
}

这本身就有效。我渲染每个评论,然后在渲染每个评论的部分内部,如果他们有任何孩子,我也会渲染孩子 cmets。

如……:

    - if !@comments.empty?
      = render partial: 'comments/comment', collection: @item.root_comments, as: :comment

但是,每个评论都可以有回复,并且这些回复可以有自己的回复(再次,如 Reddit)。因此,当我尝试对孩子的回复做同样的事情时,它会给我一个 500 错误。

= form_for([@item, @new_comment], remote: true, html: { class: "comment-reply", id: "replyto_#{comment.id}" }) do |f|
  .col-md-5
    .form-group
      = f.text_area :body, class: "form-control", rows: "3"
      = f.hidden_field :user_id, value: current_user.id
      = f.hidden_field :parent_id, value: comment.id
    .form-group
      = f.submit "Submit", class: "btn btn-sm"
  %div{style: "clear:both;"}

所以我的问题是,我如何为子 cmets 创建一个 form_for,然后(可能)创建一个新的 js.erb,这样它就不会“追加”,而是重新呈现父评论(这样会依次渲染新创建的子评论)。

我想我可能需要重新创建一个,比如 create_child,但是 form_for 会变成什么?

【问题讨论】:

    标签: javascript jquery ruby-on-rails acts-as-commentable


    【解决方案1】:

    我自己想出来的。

    基本上在 cmets_controller.rb 中,我必须找出新回复是否有 parent_id。

      def create
        @item = Item.find(params[:item_id])
        @all_comments = @item.comment_threads
        if (params[:comment].has_key?(:parent_id))
          @parent = Comment.find(params[:comment][:parent_id])
        end
        @comment = Comment.build_from(@item, current_user.id, params[:comment][:body])
        if @comment.save
          if @parent
            @comment.move_to_child_of(@parent)
          end
          respond_to do |format|
            format.js
          end
        else
          flash.now[:error] = "Comment was not submitted."
          redirect_to root_path
        end
      end
    

    然后在我的 create.js.erb 中,我需要确定它是否也有 parent_id:

    if ("<%= @comment.parent_id %>") {
        $('.comment_<%= @comment.parent_id %>').append("<%= escape_javascript(render partial: 'comment', locals: { comment: @comment } ) %>");
    
        $("#replyto_<%= @comment.parent_id %>").val('');
        $("#replyto_<%= @comment.parent_id %>").toggle();
    }
    else {
    $('#comments').append("<%= escape_javascript(render partial: 'comment', locals: { comment: @comment } ) %>");
    
        if ("<%= @comment.body %>") {
            $("#comment_body").val('');
        }
    }
    

    这允许子 cmets 被附加(通过 JavaScript)并将它们放在正确的父级下。

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2016-05-31
      相关资源
      最近更新 更多