I'm using the following for a delete link:
<%= link_to "Delete", photo_path(#photo.id), :confirm => "Are you sure?", :method=>:delete %>
Problem is, if I click CANCEL - it still deletes!!! wooops... Why is that?
First, you don't have to write photo_path(#photo.id), just pass #photo, that's enough.
<%= link_to "Delete", #photo, :confirm => "Are you sure?", :method => :delete %>
If the link doesn't work, that means that your page doesn't correctly load the JavaScript adaptator for your JavaScript framework. For example, if you use use jQuery, check this :
http://github.com/rails/jquery-ujs
Related
My View:
<%= link_to 'delete account', user_registration_path, :method => :delete, :class=>
'delete_account_button', data: {:confirm =>'sure?', disable_with: "deleting..."} %>
and I'm redirected to localhost:3000/users ... nothing more. No console errors, logs just show that I was redirected ...
FIRST WORKING EXAMPLE:
Changed link_to to button_to and it work. Why?
From the rails guides, you need a data turbo method to make it work
<%= link_to "Destroy", article_path(#article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
So update from data key with something similar
data: { 'turbo-method': :delete, 'turbo-confirm': 'Are you sure?' }
turbo documentation
button_to
Is used to submit a form and send either a POST or DELETE request
link_to
Is used redirect_to a page through GET requests
Turbo is not necessary here to make the button work. However if you wanted to reload elements on the dom you would need turbo.
Check this tutorial out if you want to learn turbo and Hotwire for Rails 7.
https://www.hotrails.dev/turbo-rails
It helped me out a lot
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to use Twitter Bootstrap Icons as Links in Ruby on Rails 3?
I have the following code to delete an item:
<%= link_to "delete", sense, :method => :delete,
:confirm => "You sure?" %>
How do I attach :method and :confirm to the icon-remove used in boostrap. I want something like:
<%= link_to <i class="icon-search"></i>, sense, :method => :delete,
:confirm => "You sure?" %>
What is the correct way to do this?
<%= link_to sense, :method => :delete, :confirm => "You sure?" do %>
<i class="icon-search"></i>
<% end %>
This should do it.
I have a nested resource attachments and I want to create a link_to to destroy/delete the attachment. Here's what I have, but it is posting as a GET versus a PUT:
<%= link_to "Delete Attachment", project_thread_attachment_path(#attachment.thread.project.id, #attachment.thread.id, #attachment.id), :confirm => "Are you sure you want to delete this attachment?", :method => :delete, :action => "destroy" %>
Ideas? Thanks!
Try
link_to "Delete Attachment", [#attachment.thread.project,#attachment.thread,#attachment], :confirm => "Are you sure?", :method => :delete
Does it work?
You should be able to use the following by itself (remove the :action => 'destroy' part). Also, the request should be a DELETE request, not a PUT request:
<%= link_to "Delete Attachment", project_thread_attachment_path(#attachment.thread.project.id, #attachment.thread.id, #attachment.id), :confirm => "Are you sure you want to delete this attachment?", :method => :delete %>
I'm diving into Ruby on Rails and I find that when I'm using the scaffold code that RoR generated for my model, it causes a confirmation box to appear that reads "Are you sure?" when I click on the delete link. What causes this to appear and how do I remove it? Thanks, I'm just trying to figure out how this all works.
The scaffold link_to will look something like this:
<%= link_to "Destroy", #code, :confirm => 'Are you sure?', :method => :delete %>
Removing the :confirm => 'Are you sure?', argument to the link helper will remove the javascript confirmation alert.
The link in the template has a :confirm=>"My message here" option. If you remove that option, then it won't have a message.
I have a link_to like this:
<%= link_to 'X', category, :confirm => 'Are you sure?', :method => :delete %>
So, I change it like this:
<%=link_to_remote category.name,
:confirm => 'Are you sure?', :method => :delete%>
it show the confirm box, but after I click "yes", it won't delete the item I want , why?
There is no information about what url you need to go to in your link_to_remote usage. You should also specify something like
:url => delete_category_path(category)
in the options hash.