简体   繁体   中英

:confirm option in Rails being ignored while using Rails 3 and jQuery UJS

I've got a table of projects in my Rails app, each with a delete link, like so:

<%= link_to "Delete", project, :confirm => "Are you sure?", :method => :delete %>

However, the delete request is sent even if the user clicks "Cancel" on the dialog box.

Any ideas on how to fix this? I'm using Rails 3, Beta 3, with the jQuery library and accompanying jQuery UJS library.

You need to get a more recent version of rails.js. I was having the same problem using the same setup you have. When I diff'd the old and new versions, I discovered the confirmation handler in the old version was commented out. The new version solves the problem.

Download rails.js from ( http://github.com/rails/jquery-ujs/blob/master/src/rails.js ) if you are using jQuery and put it in your p ublic/javascripts folder.

Then make sure you have the following on your template's header

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" %>
<%= javascript_include_tag 'rails' %>
<%= csrf_meta_tag %>

And that's it =)

I think you are using link in a Table Row. It might happen that you are using onclick function in you table row so when you cliking on Delete link Confirmation pop-up come out and when you click on cancel your onclick function is get executed.

This is an assumption but it may help you. If it does not then please edit your question with your related code.

You're going to have to post more information about your rails and more code. Since link_to works properly with the :confirm option.

I can suggest two things,

  1. Try link_to "Delete", project, { :confirm => "Are you sure?", :method => :delete } (early versions of rails didn't handle options without {} well.
  2. Look at the html generated for that fragment and see if it has the javascript, if it doesn't, try placing a plain old link_to with a confirm in another part of your code and check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM