简体   繁体   中英

How to change rails default “destroy” action in javascript?

I would like to change the default functionality Rails uses for the destroy action by adding ajax and an animation to remove the item. I know how to implement the javascript, but I'm not sure what is the best way to "tap in" or override this bit of javascript, especially now that the jquery_ujs file is vendored away somewhere due to the asset pipeline. What is the best practice for doing this?

You can still add ajax and animation without bothering about the rails default behavior or the ujs behavior.

In the destroy action, you just have to respond to a javascript request and create the necessary js file that does the animation magic using jquery.

def destroy
  # perform destructive action (optional)
  respond_to do |format|
    format.js
  end
end

Make sure there's a file called destroy.js.erb in the views/<controller_name>/ folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM