如何将一个按钮添加到部分关闭部分?

时间:2020-12-14 23:00:26

I have this partial

我有这个偏袒

_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…"

I need to add a 'Cancel' button which will remove the partial from the page (Hide and remove). The _reply.haml partial is not loaded when the page loads. It loads when someone clicks "Reply" on a comment.

我需要添加一个“取消”按钮,它将从页面中删除部分(隐藏和删除)。页面加载时不会加载_reply.haml部分。当有人点击评论的“回复”时,它会加载。

I tried adding = button_tag 'Cancel', :id => "cancel_button", :remote => true to the end of _reply.haml but it ended up going through the create action of the Comments controller for some reason. (If you click reply, it will go to the create action of the comments controller - this is correct).

我尝试添加= button_tag'取消',:id =>“cancel_button”,:remote => true到_reply.haml的结尾但是由于某种原因它最终通过了Comments控制器的创建操作。 (如果单击“回复”,它将转到注释控制器的创建操作 - 这是正确的)。

1 个解决方案

#1


2  

Actually you just need to remove the reply form which can be done using a simple js code by adding:

实际上你只需要删除回复表单,可以使用简单的js代码添加:

= button_tag 'Cancel', :id => "cancel_button"

As you added the above, to remove the div from DOM you don't need to send a AJAX request. You can just add a js code:

正如您在上面添加的那样,要从DOM中删除div,您不需要发送AJAX请求。你可以添加一个js代码:

$("#cancel_button").click(function(){
  $(".reply-form").remove();
});

So when you will click again on reply the AJAX will be fired and the partial will be loaded again in DOM.

所以当你再次点击回复时,AJAX将被触发,部分将再次加载到DOM中。

Hope this helps.

希望这可以帮助。

#1


2  

Actually you just need to remove the reply form which can be done using a simple js code by adding:

实际上你只需要删除回复表单,可以使用简单的js代码添加:

= button_tag 'Cancel', :id => "cancel_button"

As you added the above, to remove the div from DOM you don't need to send a AJAX request. You can just add a js code:

正如您在上面添加的那样,要从DOM中删除div,您不需要发送AJAX请求。你可以添加一个js代码:

$("#cancel_button").click(function(){
  $(".reply-form").remove();
});

So when you will click again on reply the AJAX will be fired and the partial will be loaded again in DOM.

所以当你再次点击回复时,AJAX将被触发,部分将再次加载到DOM中。

Hope this helps.

希望这可以帮助。