javascript - rails : acts_as_commentable_with_threading children's comment form not working

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

因此,我使用 acts_as_commentable_with_threading 来构建类似于 Reddit 的评论系统。

因此,在项目的显示页面上,我有一个包含 (items/show.html.haml) 的 form_for:

  - 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"

使用具有以下功能的 Controller (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('')
}

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

比如...:

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

但是,每个评论都可以有回复,并且这些回复可以有自己的回复(再次像 Reddit 一样)。因此,当我尝试对 child 的回复执行相同的操作时,它会给出 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;"}

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

我想我可能需要创建一个新的创建,就像create_child一样,但是form_for会变成什么呢?

最佳答案

我自己想出来了。

基本上在comments_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('');
    }
}

这允许附加子注释(通过 JavaScript)并将它们放置在正确的父注释下。

关于javascript - rails : acts_as_commentable_with_threading children's comment form not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901338/

相关文章:

javascript - 通过 GET 的关键字中的空格被 chop

ruby-on-rails - 可以生成支架将 Controller 放在命名空间中吗?

javascript - Ruby On Rails 中的页面加载不完整

ruby-on-rails - Facebook、Linkedin 和 Google 的 Oauth2 登录停止与 Devise 和 Omniauth 合作,但仍适用于 LinkedIn 和 Twitter

javascript - 以相同的形式处理javascript和php中的下拉列表操作

javascript - 如何用jquery同时将元素包裹在不同的div中

javascript - Office.js 识别文档中的 ContentControl 位置

javascript - 如何在使用 JQuery 更改 CSS 时添加淡入淡出效果

jquery - 在 Internet Explorer 9 中使用 jQuery 为子项设置动画时父 div 投影错误

javascript - 将数组发送到 JQuery 中的对象构造函数