Search code examples
ruby-on-railsruby-on-rails-5

Alert on delete in rails


I am trying to add an alert while trying to delete a document. I have created a remove action in the admin controller. The issue I am facing is that I get an alert when I click the Delete Document button but on the alert message I have to click twice on the okay button to delete the document. Please help me find the issue.

parent_controller.rb

def remove_document
    @parent.document = nil
    @parent.title = nil
    @parent.save
    flash[:success] = "Document Deleted"
    redirect_to parent_path(@parent)
end

show.html.erb

<%= link_to "Financial Document 1",  @parent.document.url %><br>
            <%= link_to 'Delete Financial Document 1', {action: :remove_document, id: @parent.id}, data: { confirm: 'Are you sure you want to delete this item?'} %>

application.js

//= require rails-ujs
//= require jquery
//= require jquery_ujs
//= require bootstrap-datepicker
//= require bootstrap-datepicker/locales/bootstrap-datepicker.en-GB.js
//= require filterrific/filterrific-jquery
//= require tinymce-jquery
//= require bootstrap
//= require activestorage
//= require turbolinks
//= require cocoon
//= require_tree .

Solution

  • Looks like you should try to remove, this you probably don't need that and jquery_ujs

    //= require rails-ujs
    

    or maybe try ther reverse and remove instead, but you may need this for bootstrap

    //=jquery_ujs
    

    Have a look at the comments here