简体   繁体   English

如何更改JavaScript中的Rails默认“破坏”操作?

[英]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. 我想通过添加ajax和动画来删除项目来更改Rails用于destroy动作的默认功能。 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. 我知道如何实现javascript,但是我不确定“插入”或覆盖这部分javascript的最佳方法是什么,尤其是由于资产管道,jquery_ujs文件被卖掉了。 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. 您仍然可以添加ajax和动画,而不必担心rails的默认行为或ujs行为。

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. 在destroy动作中,您只需要响应一个javascript请求并创建必要的js文件即可,该文件使用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. 确保views/<controller_name>/文件夹中有一个名为destroy.js.erb的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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