标准删除链接不适用于 Rails3 + jQuery UJS
我正在使用 JQuery 1.4.4 和 jQuery UJS Rails.js 制作 Ruby On Rails 3.0.3 应用程序 (https:/ /github.com/rails/jquery-ujs)
我有脚手架生成的模型/视图/控制器 - 文档,其中我在 app/views/documents/index.html.haml 文件中删除了链接:
link_to “删除”,文档,:确认=> “你确定吗?”, :method => :delete
这会生成以下 HTML:
看起来非常好。
当我点击这个“销毁”链接时,我会看到浏览器confirm(),而当我点击取消时,我预计不会发生任何事情。
但是
删除功能发生并且文档被删除。
有没有人遇到同样的问题或者我做错了什么?
目前我已经深入研究了:
我查看了jquery-ujsrails.js 并且有两个 .live() 事件附加到同一个锚点:
1) $('a[data-确认],input[data-confirm]').live('click', function () {...}
2) $('a[data-method]:not([data-remote])').live ('click', function (e){...}
第一个 live() 执行 return false; 据我了解,这应该会中断所有执行链,但事实并非如此。第二个 live( ) 仍然被执行,第二个 live() 是生成表单并提交到销毁操作的地方。
I am making Ruby On Rails 3.0.3 app with JQuery 1.4.4 and jQuery UJS rails.js (https://github.com/rails/jquery-ujs)
I have scaffold generated model/view/controller - Document, where I have delete link in app/views/documents/index.html.haml file:
link_to "delete", document, :confirm => "Are you sure?", :method => :delete
This generates this HTML:
<a href="/documents/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
Which seems very OK.
When I click this "Destroy" link then I see browser confirm() and when I click Cancel I expect to happen nothing.
BUT
Delete functionality happens and document is deleted.
Has anybody same issue or I am doing something wrong?
Currently I have digged this far:
I looked at jquery-ujs rails.js and there is two .live() events attached to same anchor:
1) $('a[data-confirm],input[data-confirm]').live('click', function () {...}
2) $('a[data-method]:not([data-remote])').live('click', function (e){...}
First live() perform return false; which as I understand should brake all execution chain, but it is not. Second live() is still executed and this second live() is where form is generated and submitted to destroy action.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
升级到最新的rails.js(适用于jQuery)应该可以解决这个问题。
https://github.com/rails/jquery-ujs/raw /master/src/rails.js
Upgrade to latest rails.js (for jQuery) shall fix the problem.
https://github.com/rails/jquery-ujs/raw/master/src/rails.js
您还应该确保您的rails.js 链接位于jquery 链接之后。我有 <%= javascript_include_tags "rails", "jquery" ....%>末端需要有铁轨。 Rails.js 使用了 jquery 创建的许多函数,因此需要在调用 Rails 之前初始化它们才能使用。
You should also make sure that your link to rails.js is AFTER your jquery links. I had <%= javascript_include_tags "rails", "jquery" ....%> and it needed rails to be at the end. rails.js uses many of the functions created by jquery so they need to be initialized before rails is called so they can be used.
我还不能投票,但我的问题与上面大卫的问题相同。
确保rails.js位于jquery链接之后。并且您的 application.html.erb 文件中也有 CSRF 标记,如下所示:
application.html.erb
有关 CSRF 的更多信息,请参见此处:
http://www.themodestrubyist。 com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/
I can't upvote yet but my problem was the same as David's above.
Make sure rails.js comes AFTER your jquery links. That and that you have the CSRF tag in your application.html.erb file also like this:
application.html.erb
More info on CSRF here:
http://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/
我有类似的错误。
首先,您需要使用button_to 来获取删除所需的POST 操作。
其次,你需要确保jquery-ujs(https://github.com/rails/jquery-ujs/raw/master/src/rails.js)所需的jquery版本是你正在使用的版本。最新版本的jquery-ujs需要jquery-1.6.0或更高版本。我使用的是旧版本。
I had similar error.
First you need to use button_to to get a POST action needed for delete.
Second, you need to ensure that the version of jquery needed by jquery-ujs (https://github.com/rails/jquery-ujs/raw/master/src/rails.js) is the version you are using. The latest version of jquery-ujs needs jquery-1.6.0 or later. I was using an older version.
当使用 Javascript 生成删除链接的 HTML 时,除了 data-method="delete" 之外,不要忘记 data-remote="true" 属性
When using Javascript to generate the HTML for the delete link, don't forget the data-remote="true" attribute in addition to data-method="delete"