我有这些文件:
_comment.haml
%div.comment{ :id => "comment-#{comment.id}" }
%hr
- if current_user && current_user.id == comment.user_id || current_user && current_user.id == reel_user
= link_to "×", comment_path(comment), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this comment?", :disable_with => "×", :class => 'close', :id => "delete_comment"
%h4
= comment.user.first_name
%small= comment.updated_at
%p= comment.body
%p= link_to "Reply", reply_comment_path(comment), :method => :get, :remote => truecomments_controller.rb
def reply
@comment = Comment.find(params[:id])
@obj = Event.find(@comment.commentable_id)
@div_id = "comment-#{@comment.id}"
respond_to do |format|
format.js
end
endreply.js.erb
$("<%= j render(:partial => 'reply', :locals => { :comment => Comment.build_from(@obj, current_user.id, ""), :parent_comment => @comment }) %>").insertAfter($('#<%= @div_id %>')).show('fast');_reply.haml
.reply-form
= form_for comment, :remote => true do |f|
= f.text_area :body, :input_html => { :rows => "2" }, :label => false
= f.text_field :commentable_id, :as => :hidden, :value => comment.commentable_id
= f.text_field :commentable_type, :as => :hidden, :value => comment.commentable_type
= f.text_field :p_comment, :as => :hidden, :value => parent_comment.id
= f.submit "Reply!", :class => "btn btn-primary", :disable_with => "Submitting…"这基本上就是如果在_comment.haml中单击"Reply“会发生的情况。如果您单击"Reply",那么来自_reply.haml的部分将在_comment.haml分部下面打开。但是,如果您多次单击"Reply“,它将继续打开更多的_reply部分。我怎样才能使它只打开表单一次,如果您再次单击它,那么什么也不会发生?
另外,如果有评论1和2,回复部分是为注释1打开的,如果您单击评论2上的“回复”,它将打开评论2的回复部分,并关闭评论1的部分。谢谢!
发布于 2015-08-24 05:59:22
有很多方法可以做到这一点。
在reply.js.erb中,在呈现表单之前,首先删除页面上的任何现有表单,此操作应防止出现多个回复表单,并从另一个回复单击中关闭回复表单。
这一行位于reply.js.erb的顶部
$('.reply-form').remove();这个答案的一个副作用是,如果一个人开始填写回复表格,然后在提交表单之前再次点击“回复”,那么他们输入的内容就会丢失。
https://stackoverflow.com/questions/32175161
复制相似问题