Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Rails:充当可注释的,带有线程子项';s的评论表单不起作用_Javascript_Jquery_Ruby On Rails_Acts As Commentable - Fatal编程技术网

Javascript Rails:充当可注释的,带有线程子项';s的评论表单不起作用

Javascript Rails:充当可注释的,带有线程子项';s的评论表单不起作用,javascript,jquery,ruby-on-rails,acts-as-commentable,Javascript,Jquery,Ruby On Rails,Acts As Commentable,因此,我将作为带有线程的\u可注释的\u使用作为类似于Reddit的注释系统 所以在一个项目的显示页面上,我有一个表单,它有(items/show.html.haml): 使用一个控制器(items\u controller.rb): 这将有一个create.js.erb,它将把新做的评论附加到页面上 $('#comments').append("<%= escape_javascript(render partial: 'comment', locals: { comment: @com

因此,我将作为带有线程的\u可注释的\u使用作为类似于Reddit的注释系统

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

使用一个控制器(items\u controller.rb):

这将有一个create.js.erb,它将把新做的评论附加到页面上

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

if ("<%= @comment.body %>") {
    $("#comment_body").val('')
}
但是,每条评论都可以有回复,这些回复也可以有自己的回复(同样,像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;"}
因此,我的问题是,我如何为子注释创建一个表单,然后(可能)创建一个新的js.erb,这样它就不会“附加”,而是重新呈现父注释(从而呈现新生成的子注释)

我想我可能需要做一个新的创造,就像一个创造的孩子一样,但是这个形式会变成什么呢?

我自己想出来了

基本上在comments\u controller.rb中,我必须找出新回复是否有父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中,我需要找出它是否也有父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('');
    }
}
if(“”){
$('.comment')。附加(“”);
$(“回复到”)val(“”);
$(“#replyto”)toggle();
}
否则{
$(“#注释”)。追加(“”);
如果(“”){
$(“#评论"正文”).val(“”);
}
}
这允许附加子注释(通过JavaScript),并将它们放在正确的父注释下

= 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;"}
  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
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('');
    }
}