首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使单击link_to只打开一次部分?

问如何使单击link_to只打开一次部分?
EN

Stack Overflow用户
提问于 2015-08-24 05:46:16
回答 1查看 43关注 0票数 0

我有这些文件:

_comment.haml

代码语言:javascript
复制
%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

代码语言:javascript
复制
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

代码语言:javascript
复制
$("<%= j render(:partial => 'reply', :locals => { :comment => Comment.build_from(@obj, current_user.id, ""), :parent_comment => @comment }) %>").insertAfter($('#<%= @div_id %>')).show('fast');

_reply.haml

代码语言:javascript
复制
.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的部分。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-24 05:59:22

有很多方法可以做到这一点。

在reply.js.erb中,在呈现表单之前,首先删除页面上的任何现有表单,此操作应防止出现多个回复表单,并从另一个回复单击中关闭回复表单。

这一行位于reply.js.erb的顶部

代码语言:javascript
复制
$('.reply-form').remove();

这个答案的一个副作用是,如果一个人开始填写回复表格,然后在提交表单之前再次点击“回复”,那么他们输入的内容就会丢失。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32175161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档