[英]How do I make clicking a link_to open up a partial only once?
I have these files: 我有这些文件:
_comment.haml _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 => true
comments_controller.rb comments_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
end
reply.js.erb Reply.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.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…"
That's basically the flow of what happens if you click "Reply" in _comment.haml. 如果您在_comment.haml中单击“答复”,基本上就是这样。 If you click "Reply", then the partial from _reply.haml opens up underneath the _comment.haml partial. 如果单击“答复”,那么_reply.haml中的部分将在_comment.haml部分下面打开。 However, if you click "Reply" more than once it will continue opening more _reply partials. 但是,如果多次单击“答复”,它将继续打开更多的_reply局部。 How can I make it so that it only opens the form once and if you click it again then nothing happens? 我怎样才能使它只打开一次表单,如果再次单击它,什么也没发生?
Also, how can I make it so that if there's comment 1 and 2 and the reply partial is open for comment 1, if you click "Reply" on comment 2, it will open up the reply partial for comment 2 and close the partial for comment 1. Thanks! 另外,我该如何做,以便如果有评论1和2,并且对评论1的答复部分是打开的,如果在评论2上单击“答复”,它将打开对评论2的答复部分,并为评论2关闭部分评论1.谢谢!
There's lots of ways to do this. 有很多方法可以做到这一点。
In reply.js.erb
before rendering the form, first remove any existing forms on the page, this action should prevent multiple reply forms coming up, and close reply forms from another reply click. 在呈现表单之前,在reply.js.erb
,首先删除页面上的任何现有表单,此操作应防止出现多个答复表单,并从另一个单击答复中关闭答复表单。
this line goes at the top of reply.js.erb
该行位于reply.js.erb
的顶部
$('.reply-form').remove();
One side-effect of this answer is that if a person starts filling in the reply form and then they click on "Reply" again before they submit the form, then what they've typed will be lost 此答案的一个副作用是,如果某人开始填写答复表单,然后在提交表单之前再次单击“答复”,则输入的内容将会丢失
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.