Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails confirm before delete

Here is my rails link_to

<%= link_to 'Delete',url_for(action: :delete,id: @user.id),data: {confirm: "Are you sure?"} %>

I tried the above method but it is directly deleting without any alert message. What wrong I made. Can any one correct it.

like image 924
Prabhakaran Avatar asked Oct 25 '13 11:10

Prabhakaran


3 Answers

Try this

<%= link_to 'Delete',url_for(action: :delete,id: @user.id),method: :delete, data: {confirm: "Are you sure?"} %>
like image 151
userxyz Avatar answered Oct 21 '22 16:10

userxyz


Answer for rails 4.1.8 (question doesn't include version)

and standard resource (also not specified)

  1. //= jquery_ujs must be included in application.js (possibly missing)
  2. url is user_path(@user)
  3. specify :method => :delete (missing)
  4. confirm message within :data (was good)
  5. destroy method in controller (not delete)

= link_to t(:delete) , user_path(@user), :method => :delete, :class => "btn btn-danger", :data => {:confirm => t(:are_you_sure )}

The url in the question seems to create a GET, rather than a DELETE method. This may work if :method was specified. imho it is unfortunate that there is no seperate named url helper for this.

like image 11
Torsten Avatar answered Oct 21 '22 14:10

Torsten


Before Rails 7

<%= link_to 'Delete', url_for(action: :delete, id: @user.id),
  method: :delete, data: {confirm: "Are you sure?"} %>

Rails 7 (with Turbo, out of the box)

<%= link_to 'Delete', url_for(action: :delete, id: @user.id),
  data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
like image 4
installero Avatar answered Oct 21 '22 14:10

installero