0

This is the controller code that I'm using for the delete action in my Rails application. I'm not using a separate view for the delete action.

def delete
    @industry = Industry.find(params[:id])
    @industry.destroy
    flash[:notice] = "Industry successfully deleted"
    redirect_to(:action => 'index')
  end

The action is triggered and executed in the index view itself, however I wish to add two things:

  1. How can I create a JavaScript alert that warns the user before deleting the object?
  2. The flash message stays at the top of the index page. How can I add JavaScript so that the flash message box appears and vanishes after 10 seconds?

Basically, how do I intercept the destroy action in Rails 4?

1
  • 1
    This: Basically how do I intercept the destroy action in Rails 4. has nothing to do with the rest of your question. You're not "intercepting the destroy action in rails", you are asking the user if they want to click that link. When they click it, it sends a request to your server, which then makes the rails destroy action happen. Commented Mar 12, 2014 at 16:52

2 Answers 2

2

To remove the flash after 10 seconds:

$('.your-alert-div').delay(10000).fadeOut(400); // The fadeOut is just a transition I added. you can do remove() instead

Similarly, you can use setTimeout.

window.setTimeout(function() {
  $(".alert").fadeTo(500, 0).slideUp(500, function(){
      $(this).remove();
  });
}, 6000);

To ask a user through a JavaScript alert, before they delete something, you can do something like this to your link:

<%= link_to "Delete", object, :method => :delete, :data => { :confirm => "Are you sure?" } %>
Sign up to request clarification or add additional context in comments.

8 Comments

I'm a little lost here, where do I intercept the delete action and put this code, since I'm not using a separate view for the delete action.
To delete the object, I'm assuming you have a link that a user clicks to destroy it right? Like @meagar says, by the time the Destroy action is invoked, it's too like to add JavaScript to alert the user. That's why you can do in front-end with the data-confirm message.
Yep replaced my delete call on the index.html.erb with this code, but it doesn't work. What does 'path' mean anyway here?
Object, not path. In your case, from what I can tell from your destroy action, it'd be @industry or industry
Since you didn't post up any of your view code, Justin is giving an example of the sort of thing you want to do. You need to "translate" the example to use the variable names, div ids, classes, etc you are using on the page.
|
1

The alert has nothing to do with this action. You need to render that JavaScript in the previous page, the one that makes a request to this action.

By the time the action is invoked, it's too late to prompt the user.

The notice is similarly unrelated to this action. You need some JavaScript which is attached to your layout, which uses setTimeout to hide the notice element when its present in the DOM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.