rails confirm before delete - ruby-on-rails

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?"} %>

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)
url is user_path(#user)
specify :method => :delete (missing)
confirm message within :data (was good)
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.

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?'} %>

link_to('Delete', {controller: :controller_name, id: id, action: :action_name}, confirm: "Are you sure you want to delete this?", method: :delete)

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 %>

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

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' %>
Cancel
</div>
</div>
</div>
</div>
</div>
</td>

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?" }} %>

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.

Related

Rails delete link url took wrong page

If I delete a post, it takes me to post/index.html.erb, but I want it to redirect to the profile/index.html.erb page.
<% current_user.posts.each do |post| %>
<%= post.post_name %> </p>
<%= link_to "view", post, class: "btn btn-default" %>
<%= link_to "Delete", post_path(post), method: :delete,
data: { confirm: "Are you sure?" },
class: "btn btn-default" %>
<% end %>
You're looking at the wrong code. Your view has nothing to do with this. Fix/change the redirection in your PostsController#destroy.

Rails pass radio button params

Hey guys I am a newbie so go easy on me.
I am trying to figure out how to pass to the controller the radio button choice. It is NOT part of the model. Here is the code:
<%= radio_button_tag(:remove, "yes") %>
<%= label_tag(:yes_delete, "yes, delete the order") %>
<%= radio_button_tag(:remove, "no") %>
<%= label_tag(:no_dont_delete, "No, do not delete the order") %>
<h4 class="row"><td><%= link_to 'Delete', payment_path(:id => #payment.id), method: :delete, data: { confirm: 'Are you sure?' } %></td></h4>
When I click delete all here is what the params look like:
Parameters: "authenticity_token"=>"lfFsfN04ajhIpYI13bwhmGtLtQIdRUkaClPsBBP12SSZwDEnsIslOqH3yptGvWUF620bEYhPBgbD7sLYV2cA7Q==", "id"=>"175"}
Thank for the help.
form_for can handle that easily.
<%= form_for #payment, method: :delete do |f| %>
<%= radio_button_tag(:remove, "yes") %>
<%= label_tag(:yes_delete, "yes, delete the order") %>
<%= radio_button_tag(:remove, "no") %>
<%= label_tag(:no_dont_delete, "No, do not delete the order") %>
<h4 class="row"><td><%= f.submit 'Delete', data: { confirm: 'Are you sure?' } %></td></h4>
<% end %>
The post parameters received by the server:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KoCb84m88ptGUPbY9eofUMjpGLGmpKfUwBxB2t+Fv+QABr6lWnWpsEuJQ/quOJN3g6Zm+DZy2JTdVokjMQxLvQ==", "remove"=>"yes", "commit"=>"Delete", "id"=>"1"}

Passing HTML from serverside to datatables

I am changing my Datatables to load content from my Rails app. In one of the cells I have a big chunk of HTML, and since when using the server-side approach the cells are printed with Javascript, I would need to pass the HTML from Rails.
So, here is how it looks one of the cells with datatables WITHOUT server side fetching:
<td>
<div class='order-actions-container'>
<div class='order-action'>
<%= link_to 'Show pages', admin_order_pages_path(order.id), :class => 'btn btn-primary' %>
</div>
<% if order.status.name == 'reviewed' %>
<div class='order-action'>
<%= form_tag(admin_order_set_completed_status_path(order), :method => 'patch' ) do %>
<%= submit_tag 'Complete order', class: 'btn btn-success', data: { confirm: 'Are you sure you want to complete this order?' } %>
<% end %>
</div>
<% end %>
<% if order.status.name == 'queued' %>
<div class='order-action'>
<%= form_tag(admin_order_process_order_path(order), :method => 'post' ) do %>
<%= submit_tag 'Process order', class: 'btn btn-success', data: { confirm: 'Are you sure you want to process this order?' } %>
<% end %>
</div>
<% end %>
<% if order.status.name != 'processing' %>
<div class='order-action'>
<%= form_tag(admin_order_path(order), :method => 'delete' ) do %>
<%= submit_tag 'Delete order', class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this order?' } %>
<% end %>
</div>
<% end %>
</div>
</td>
Now, since all the data will come back from Rails, I would need to pass that HTML via a JSON. Isn't that breaking the MVC? How do you deal in these situations?
If you are wanting to use json with ajax for datatables you can use JBuilder or rabl to create a json document that can be easily reused.
Also there is not a need to write out form_tags wrapped around submit_tags to call different methods, with rails you can use link_to and specify the :method to accomplish the same thing.
<td>
<div class='order-actions-container'>
<div class='order-action'>
<%= link_to 'Show pages', admin_order_pages_path(order), :class => 'btn btn-primary' %>
</div>
<% if order.status.name == 'reviewed' %>
<div class='order-action'>
<%= link_to "Complete order", admin_order_set_completed_status_path(order), method: :patch, class: 'btn btn-success', data: { confirm: 'Are you sure you want to complete this order?' }%>
</div>
<% end %>
<% if order.status.name == 'queued' %>
<div class='order-action'>
<%= link_to "Process order", admin_order_process_order_path(order), method: :post, class: 'btn btn-success', data: { confirm: 'Are you sure you want to process this order?' }%>
</div>
<% end %>
<% if order.status.name != 'processing' %>
<div class='order-action'>
<%= link_to "Delete order", admin_order_path(order), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this order?' }%>
</div>
<% end %>
</div>
</td>

Ruby in Rails, destroy does not work in edit. routing to show

I have a problem with the destroy action.
It works in the index.html.erb very fine.
But I can't get it work in the edit.html.erb
It routes to the show even without asking for a permission.
Many solutions here say it has something to do with jQuery. Well as you can see, I tried all I could find.
<%= stylesheet_link_tag "application" %>
<!--Delete Problem in Edit-->
<%#= javascript_include_tag :all %>
<%#= javascript_include_tag :application %>
<%#= javascript_include_tag :default %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Is there a solution that works for my situation?
This is my delete button in edit:
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>
<%= t "list.button_delete" %>
<%#= link_to I18n.t(".list.delete"), contact, confirm: 'Are you sure?', method: :delete %>
<%#= link_to 'delete', contact_path(#contact), :method => :delete %>
<% end %>
I tried many things, as well.
The following is the action:
def destroy
##contact = Contact.find(params[:id])
#contact = current_user.contacts.find(params[:id])
#contact.destroy
redirect_to contacts_url,
alert: 'Successfully deleted the contact'
end
Replace
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>
with
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", data: {confirm: 'Are you sure?'}, data: {method: :delete}) do %>
It should be data: {confirm: 'Are you sure?'} instead of confirm: 'Are you sure?' and also data: {method: :delete} instead of method: :delete .
where, link_to method interpolates confirm: 'Are you sure?' as data-confirm="Are you sure?" and method: :delete as data-method="delete".
BUT content_tag method interpolates confirm: 'Are you sure?' as confirm="Are you sure?" and method: :delete as method="delete" so your javascript call is not getting invoked.
In your destroy action replace:
#contact = current_user.contacts.find(params[:id])
with:
#contact = #current_user.contacts.find(params[:id])

Adding a class to link_to block

I have a following code which displays a 'delete' link:
<%= link_to :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
But for some reason ROR is not adding some_class to a tag. Have you any idea what can i do to fix it ? Thanks in advance.
You need to add the URL as the first parameter, then the html options, e.g.:
<%= link_to resource_path(#resource), :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
I actually found this to be a working solution with Rails 4.2
<%= link_to(resource_path(#resource), class: "project-card clearfix") do %>
<h1>Your html here</h1>
<% end %>
If you need to pass a controller and action, like edit and destroy, do it as follow:
<%= link_to url_for(controller: controller_name, action: :edit, id: item.id), class: "btn btn-link btn-warning btn-just-icon edit" do %>
<i class="material-icons">edit</i>
<% end %>
<%= link_to url_for(controller: controller_name, action: :destroy, id: item.id), method: :delete, data: { confirm: t('common.confirm') }, class: 'btn btn-link btn-danger btn-just-icon remove' do %>
<i class="material-icons">close</i>
<% end %>
The link_to docs:
link_to(body, url, html_options = {})
So you'd want
<%= link_to <span>Delete</span>, '/someurl', :class=>'some_class', :method=>:delete, .... %>

Resources