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
Related
So Rails 7 changed the way that JavaScript is handled, replacing the UJS functionality with turbo components.
As part of this change, the syntax to create a link that emits a DELETE request has changed.
Rails 6 example:
<%= link_to 'Delete', author_path(author), method: :delete, data: { confirm: 'Are you sure?' } %>
Rails 7 example:
<%= link_to 'Delete', author_path(author), data: {turbo_method: :delete, turbo_confirm: "Are you sure?" } %>
This means that during an update to Rails 7 all these types of calls using the link_to helper need to be identified and adapted.
DHH has written a blog post on the changes, but I don't see the rationale that necessitates the changed syntax in the link_to helper here.
Am I missing something?
I am trying to delete a record from my database using Rails destroy action and button_to, however, the confirmation does not pop up and the app simply deletes the record without confirmation. I've tried multiple approaches:
<%= button_to 'Delete', post_path, :method => :delete, :confirm => 'Are you sure you want to delete this item?' %>
<%= button_to 'Delete', post_path, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= button_to 'Delete',post_path, :method => :delete, data: { confirm: 'Are you sure you want to delete this item?'}
<%= button_to 'Delete', post_path, {method: :delete}, {confirm: 'Are you sure?'} %>
Neither shows a confirmation module.
I was able to get this to work by following this here.
https://dev.to/software_writer/how-to-show-a-delete-confirmation-dialog-in-rails-using-stimulus-17i
Basically what you want to do is create a Stimulus js file in your app/javascript/controllers. In your case it would be posts_controller.js. Then put the following code in posts_controller.js.
import { Controller } from "#hotwired/stimulus"
export default class extends Controller {
// method for alerting before deleting posts
delete(event) {
let confirmed = confirm("Are you sure?");
if(!confirmed) {
event.preventDefault();
}
}
}
When you create the button to delete your post, have the parent div container have the data-controller="posts" attribute. Then set the action for the button_to to your method in your posts_controller.js. It should look something like this.
<div id="postDeleteLink" data-controller="posts">
<%= button_to "Delete this post", #post, method: :delete, data: { action: "click->posts#delete" } %>
</div>
This is my link_to method:
<%= link_to 'Dispatch Now', order_path(order["objectId"]), :method => 'delete', :confirm => 'Are you sure?' %>
I've looked at my old ruby on rails 3 projects and this is how I've called confirm in my link_to helpers.
It doesn't seem to have any effect.
Has something changed with ruby on rails 4 that would have caused this to stop working? I have the jquery-rails file in my gemfile and I've checked my application.js file and everything looks fine.
What could be wrong?
You'll need to use:
<%= link_to "Delete", path, method: :delete, data: { confirm: "Are you sure?"} %>
--
Rails 4 changed the syntax, so that the confirm attribute is now handled in the data hash. Whereas before you could get away with confirm:, now you have to manage data: { confirm: "your_confirmation" }
With rails 2.3.8 I had
= link_to 'old_Delete', link_path(#link), :confirm => 'Are you sure?',
:method => :delete
With rails 3.1.8 this no longer works, no error and the log just shows a show request.
I tried changing it to
= link_to 'Delete', link_path(#link), method: :delete,
data: { :confirm => 'Are you sure?' }
but that didn't help either.
How can I have a delete link that works.
I created a new app (rails 3.2.7) with a 'post' resource to see how links look in a new app.
The link generated for that was:
= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' }
but when I try to use that format, replacing post (Post resource, model, etc.) with #link, the link instance that I am using elsewhere on the page, i.e.
= link_to 'Destroy', #link, method: :delete, data: { confirm: 'Are you sure?' }
It doesn't work (no error).
I tried saving rails.js in my public/javascripts folder (I have assest compilation turned off) and including config.action_view.javascript_expansions[:defaults] = %w(jquery rails) in my config/application.rb but it didn't help, still seeing gets and no deletes happening.
As others have said you probably have a typo, link vs #link. But the reason the delete stopped working is probably centered around the change to UJS. Long answer, google "rails change to ujs in 3.0". Short answer, make sure your app is loading rails.js from an appropriate ujs helper provider, such as https://github.com/rails/jquery-ujs
Make sure that, along with your other changes you change:
= javascript_include_tag "application"
to
= javascript_include_tag :defaults
In the last approach is your variable "link" or "#link"? Could you try using the later one?
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.