简体   繁体   English

我如何单击一次link_仅打开一次局部视图?

[英]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.

相关问题 如何让网页只显示一次? - How do I make a web page show up only once? Javascript,如何让向上键只按一次 - Javascript, how do I make the up key only pressed once 如何建立链接,以便单击该链接会触发GET而不打开窗口 - How to make a link so that clicking it fires a GET but do not open up a window 如何让我的密码提示只在我打开我的网站时出现一次,而不是每次我重新加载页面或重新访问它时出现 - How do I make my password prompt only show up once when I open up my website instead of every time I reload the page or revisit it 如何在加载时打开javascript div但在单击另一个div链接时隐藏它? - How do I make a javascript div open on load but hide when clicking on another div link? 如何创建仅在我单击的div上打开的灯箱? - How do I make this lightbox I am creating only open on the div I am clicking? 如何使PDF图像在新的弹出窗口中打开其链接? - How do I make an image in PDF open its link in new pop-up window? 如何在没有link_to的情况下实现远程:true功能? - How do I implement remote: true functionality without link_to? 如何只显示一次弹出窗口? - How do I show a pop up only once? 如何为&lt;%= link_to%&gt;正确使用转义javascript? - How do I properly use escape javascript for <%= link_to %>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM