删除/销毁在使用 jQuery 的 Rails 3 中不起作用
我的删除/销毁不适用于 Rails 3。
不适用于任何脚手架,甚至不适用于新项目。
<%= link_to 'Destroy', card, :confirm => 'Are you sure?', :method => :delete %>
来自这个问题。解决方案是重新安装 Firefox。但我的也无法在 chrome、safari 或 opera 中工作。
生成的 Html 代码:--
<a href="/categories/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
PS:请不要说包含默认 JS 文件或其他内容。因为我对原型不感兴趣,因为我使用的是 jQuery。
编辑/更新,重要:当您根本不想使用原型时,这是解决方案。我在我的项目中仅使用 jQuery 和相应的插件。
人们的回答是:首先包含prototype等,然后安装一些gem等来消除prototype和jQuery之间的冲突。那是垃圾。
我已经发布了答案。请在选择该选项之前检查一次。为我工作了 10 多个项目,没有任何问题。您需要做的就是:
从 javascript 目录中删除除 application.js 之外的所有 js 文件。然后将我在答案中指定的代码粘贴到新文件中并包含该文件。包含 Jquery.js 然后一切就都准备好了。您不需要添加默认的 javascript(即:原型)或其他一些 gem 来消除冲突等。
My delete/destroy is not working for Rails 3.
Not for any scaffold or even for new projects.
<%= link_to 'Destroy', card, :confirm => 'Are you sure?', :method => :delete %>
From this question. Solution is Firefox reinstalation. But mine is also not working in chrome, safari or opera.
Html code generated:--
<a href="/categories/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
PS: Please don't say include default JS files or something. Because I am not interested in prototype all together, as I am using jQuery.
EDIT/Update, Important: this is the solution when you don't want to use prototype at all. I am using only jQuery and respective plugins in my project.
People are answering: First include prototype etc and then install some gem etc to remove conflicts between prototype and jQuery. That's rubbish.
I have posted an answer. Please check that once before you go for the option. Worked for me for more than 10 projects without any issues. All you need to do is:
Remove all the js files from your javascript directory except application.js. Then Paste the code i specified in my answer in a new file and include that file. Include Jquery.js and then you are all set. You don't need to add default javascript (ie: prototype) or some other gem to remove conflicts etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了 Mohit 遇到的同样问题,并且还需要在我的 JavaScript 资产中包含“Unobtrusive JavaScript Library”(或“ujs”)。在我当前的 Rails (v3.2.5) 中,将自动提供 UJS 库。您可以通过查看 Gemfile 中的以下行来验证这一点:
gem 'jquery-rails'
以及 app/assets/javascripts/application.js 文件中的以下行:
//= require jquery_ujs
由于我不太了解,我从自己的 application.js 文件中删除了
require jquery_ujs
行,我花了一段时间才弄清楚为什么我的link_to ..., :方法 => :delete
调用不再有效!一旦我理解了问题,就很容易将上述两行添加回各自的文件中,并且一切都再次开始按预期工作。
I ran into the same issue Mohit had and also needed to include the 'Unobtrusive JavaScript Library' (or 'ujs') in my JavaScript assets. In my current Rails (v3.2.5), the UJS library will be provided automatically. You can verify this by seeing the following line in your Gemfile:
gem 'jquery-rails'
and the following line in your app/assets/javascripts/application.js file:
//= require jquery_ujs
Since I didn't know any better, I had removed the
require jquery_ujs
line from my own application.js file, and it took me a while to figure out why mylink_to ..., :method => :delete
calls weren't working any more!Once I understood the problem, it was easy to add the above two lines back to their respective files and everything started working as expected again.
如果您使用 jQuery 而不是 Prototype,那么您需要在项目中添加 Jquery.rails.js,否则每次您尝试删除任何内容时都会显示页面。
我不记得从哪里得到解决方案和这个 Jquery.rails.js 文件。但肯定来自一些值得信赖的来源。
以下是该文件的代码:
更新
您可以从此处获取 Jquery.rails.js 的最新副本。
If you are using jQuery not Prototype then you need to add Jquery.rails.js in your project, otherwise every time you try to delete anything it will take you to show page.
I don't remember from where I got the solution and this Jquery.rails.js file. But sure from some trustworthy source.
Here is the code for that file:
Update
You can get latest copy of Jquery.rails.js from here.
确保您的布局中包含默认的 Rails javascript 文件。
Make sure that you include default Rails javascript files in your layout.
确保您的布局中包含默认的 Rails javascript 文件。
Make sure that you include default Rails javascript files in your layout.