1

Is there any simple way to pass parameters to destroy confirm button?

I don't want to display only "Are you sure?" message with OK/Cancel buttons. I want to pass some parameters which will be saved as a comment to the deleted object.

I would like to get to a result similar to the picture:

enter image description here

Jan Krupa
  • 484
  • 9
  • 21

1 Answers1

1

1 => suppose you have a link of delete object

<%= link_to 'Delete', 'javascript:;',id: "#{obj.id}", class: 'delete_object_by_audit_cmt'%>

2 => On click delete link open a model with with comment field. and also pass id of object which will be deleted.

<script>
  $('.delete_object_by_audit_cmt').on('click', function{
    var id_of_obj = $(this).attr('id');
    $('#deleted_obj_val').val(id_of_obj); // set hidden field id value
    $('#modal_id').show();
  });
</script>

3 => Create a model with with form field (watch only steps, you should wrap this in to bootstrap modal with a modal id)

<div id="modal">
  <p>Are you sure want to delete ... your custom message</p>
  <%= form_tag delete_object_path%>
    <%=text_field_tag :audit_comment%>
    <%= hidden_field_tag :id, "", id: 'deleted_obj_val'%>
    <%= submit_tag :"Yes delete"%>
    <%= link_to 'Cancel', cancel_path%>
  <%end%>
</div>

Summary:- 1) on delete link click open a modal

2) set that modal's id's hidden field value to deleted object value.

3) on click yes delete it will submit audit msg as well as id of object and you can handle it at controller side.

Anand
  • 6,457
  • 3
  • 12
  • 26
  • I don't really know how to get this going. I have to dive into the javascript. What I want to do is to define a global function for all classes, which will define this popup window (so I just pass an instance to this function and that's it). But I never worked with javascript so It will take a time. – Jan Krupa Jun 25 '18 at 12:55
  • 1
    @jan krupa Exactly I have done. You can look around the script code which mean that I have defined a common class for all delete link with dynamic id containing value of object which will be deleted . So here by on click delete link , a click event will be fired with common class and with that common class , id value of object will be obtained with $(this).I'd And so after that modal will be poped up and hidden field value will be set with I'd of object which will be deleted that is stored in id_of_obj. So.. on . I hope it's clear to you. Let me know for further guidance. – Anand Jun 25 '18 at 13:13
  • I don't really know where should be the `div` and the `script` defined so it can be called from anywhere. – Jan Krupa Jun 26 '18 at 07:03
  • Well, it works, but I have to add some "workarounds" so now it looks like a total mess. – Jan Krupa Jun 26 '18 at 12:25
  • @jan Great to know that. – Anand Jun 26 '18 at 13:53
  • I have to call two helper methods on every page I want to use this. One is for triggering the modal. Second is for the common modal window at the page. Then I've written some javascript, which will pass data related to row, to triggered modal window. It's overcomplicated, but I did not want to have a lot of modals on the page for every row. It's a little bit more complicated because the modal window is used for every controller and even for nested routing. – Jan Krupa Jun 27 '18 at 07:45