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.
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.
Try this
<%= link_to 'Delete',url_for(action: :delete,id: @user.id),method: :delete, data: {confirm: "Are you sure?"} %>
data
hash is the only option in the provided solutions here that ended up working for me. Rails 4.1.2, Ruby 2.1.2p95, jquery-rails 3.1.1. That said, in other apps, I've used the confirm
option outside the data
hash, as an argument to link_to, and it has totally worked. What gives? –
Ahvenanmaa Answer for rails 4.1.8 (question doesn't include version)
and standard resource (also not specified)
//= jquery_ujs
must be included in application.js (possibly missing)user_path(@user)
:method => :delete
(missing):data
(was good)= 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.
<%= link_to 'Delete', url_for(action: :delete, id: @user.id),
method: :delete, data: {confirm: "Are you sure?"} %>
<%= link_to 'Delete', url_for(action: :delete, id: @user.id),
data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
The answer by @installero didn't work for me in rails 7.0.3, I achieved a similar thing using button_to
:
<%= button_to "Delete", @category, form: { data: { turbo_confirm: "Are you sure?" } }, method: :delete %>
button_to
helper, the asnwer was about link_to
though –
Sicard link_to('Delete', {controller: :controller_name, id: id, action: :action_name}, confirm: "Are you sure you want to delete this?", method: :delete)
since Rails 7 Hotwire Turbo was introduced
<%= link_to "Delete", @post, class: 'btn btn-danger',data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
<%= button_to "Destroy this post", @post, method: :delete, class: 'btn btn-danger', form: { data: { turbo_confirm: "Are you sure?" }} %>
Rails confirmation popup box for destroy action:
<%= link_to 'Show', article_path(article),{:class=>'btn btn-success show_article' }%>
<%= link_to 'Edit', edit_article_path(article),{:class=>'btn btn-warning edit'} %>
<%= link_to 'Destroy', '#', "data-toggle"=>"modal", "data-target" => "#delete-#{article.id}",:class=>'btn btn-danger' %>
<div class="modal fade" id="delete-<%= article.id %>" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">Confirmation Box</button>
</div>
<div class="modal-body">
<p>Are you sure to delete this article</p>
</div>
<div class="modal-footer">
<div class="modal-footer">
<%= link_to 'Delete', article_path(article), method: :delete, :class => 'btn btn-danger' %>
<a href="#" data-dismiss="modal" class="btn btn-warning">Cancel</a>
</div>
</div>
</div>
</div>
</div>
</td>
I think you should use :method option
<%= link_to 'Delete',url_for(action: :delete,id: @user.id), method: :delete, confirm: "Are you sure?" %>
It's probably better to use button (and form) for this kind of action
data-confirm="Are you sure?"
and data-method="delete"
–
Thresathresh Check the following link:
<%= link_to 'Delete',url_for(action: :delete,id: @user.id), confirm: "Are you sure?" %>
Make sure you have both the jQuery library and the jQuery driver for Rails included. The jquery-rails
gem will take care of both of these in the later versions of Rails, from what I know.
Also, for better chances of compatibility with future versions of the jQuery driver, move the :confirm
option outside :data
and let the jquery-rails
gem decide what to do with it.
© 2022 - 2024 — McMap. All rights reserved.