gpt4 book ai didi

javascript - rails 4,当需要 jquery,jquery_ujs 时,link_to 无法使用销毁操作

转载 作者:数据小太阳 更新时间:2023-10-29 05:30:03 25 4
gpt4 key购买 nike

为什么在 application.js 文件中需要 jquery 后销毁操作不兼容?如何在不忽略 jquery 的情况下使销毁操作再次起作用?

帖子索引 View :

h1 Blog
- @posts.each do |post|
h2 = link_to post.title, post
p = post.content
p = link_to 'Edit', edit_post_path(post)
p = link_to 'Delete', post, data: {confirm: "Are you sure?"}, method: :delete
br

p = link_to 'Add a new post', new_post_path

销毁帖子 Controller 中的 Action :

def destroy
@post = Post.find params[:id]
@post.destroy

redirect_to posts_path, :notice => "Your post has been deleted"
end

应用程序.js:

= require jquery
= require jquery_ujs
//= require turbolinks
//= require_tree

一旦我将这两个注释掉,销毁操作就会再次起作用。如果没有评论,删除链接只会触发显示操作……你的想法是什么?

最佳答案

所以 Assets 管道对这些评论做了一些特别的事情。存在表单的评论

//= require jquery

指示预处理器在构建 application.js 时包含该文件以提供给应用程序的客户端。当您删除评论时,您将删除此行为 - 生成的库不会编译到应用程序提供的最终 application.js 中。

这样做的结果是您误解了 required 指令的工作方式。它只在评论中有效。当您删除注释标签时,您将停止包含该库。

关于javascript - rails 4,当需要 jquery,jquery_ujs 时,link_to 无法使用销毁操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com