Search code examples
jqueryruby-on-rails-3ujs

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" 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.


Solution

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

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