Search code examples
ruby-on-railsrubyacts-as-audited

Destroy confirmation popup with input


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


Solution

  • 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.