开发者

Standart Delete link not working with Rails3 + JQuery UJS

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开发者_C百科" 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.


Upgrade to latest rails.js (for jQuery) shall fix the problem.

https://github.com/rails/jquery-ujs/raw/master/src/rails.js


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.


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

  <head>
<title><%= title %></title>
<%= render 'layouts/stylesheets' %>
<script src="http://jquery.com/src/jquery-latest.js"></script>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag :all %>
<%= csrf_meta_tag %>

More info on CSRF here: http://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/


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.


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜