Ruby Rails delete confirmation not popping up before destroy - ruby-on-rails

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>

Related

Rails 7 - Devise do not delete user account

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

Delete link sends Get request instead of Delete Rails 7

<%= link_to 'Delete', [task.list, task],
method: :delete,
data: { confirm: 'Are you sure?' } %>
Started GET "/lists/2/tasks/4" for 127.0.0.1 at 2022-05-24 13:32:16 +0500
AbstractController::ActionNotFound (The action 'show' could not be found for TasksController)
Like #TTD said, you need to do something like this: <%= link_to "Delete", task_path(#path), method: :delete, data: { confirm: "Really?" } %> 0
as you need to reference just one object
You need to replace [task.list, task], with the path like so: task_path(task) (or whatever your resource path is)

ruby-on-rails - custom method and link_to

I have a program with users and projects (many to many relation). I would like to create my own methods: to delete all projects from specific user and to delete specific project from specific user, but I can't handle that. There is (quiet big) possibility I don't understand routes.
Below I insert code to delete all project from specific user.
In my user_controller.rb I have:
def delete_projects_from_user
#user.projects.delete_all
end
In show.html.erb link_to:
<%= link_to 'Delete all projects', #user, method: :delete_projects_from_user, data: { confirm: 'Are you sure?' } %>
And in routes I tried among others this two option:
resources :users do
get 'delete_projects_from_user', on: :member
end
or
post '/users/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
First option trows: "No route matches [POST] "/users/(id)"
Second option just do nothing.
I will be grateful for prompt.
Basic format:
<%= link_to 'DISPLAY TEXT', YOUR_ROUTE_path(#object), method:
:HTTP_METHOD, data: { Additional html params } %>
Here is the solution:
<%= link_to 'Remove All Projects', delete_projects_from_user_path(#user), method: :post, data: { confirm: 'Are you sure?' } %>
Then in your method:
def delete_projects_from_user
user = user.find(params[:id])
user.projects.delete_all
redirect_to :back #if nothing to render
end
I'm sure this may help you.
For this
def delete_projects_from_user
#user.projects.delete_all
end
You better user .destroy_all to make sure this object and all of it's associated items are destroyed as well
.delete_all only delete the object and it leaves the associated entries on the DB
and As for this:
<%= link_to 'Delete all projects', #user, method: :delete_projects_from_user, data: { confirm: 'Are you sure?' } %>
In your route you defined your route as post so it should be
method: :post
to be like this
<%= link_to 'Delete all projects', #user, method: :post, data: { confirm: 'Are you sure?' } %>
And here you haven't added the route correctly, it should be like this
<%= link_to 'Delete all projects', YOUR_ROUTE_path(#user), method: :post, data: { confirm: 'Are you sure?' } %>
While it's recommended to define this route like this
delete '/users/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
As for the 2nd option, you can use collection as well
resources :users do
collection do
delete 'user/:id', to: 'users#delete_projects_from_user', as: :delete_projects_from_user
end
end
and modify the link to be
<%= link_to 'Delete all projects', delete_projects_from_user(#user), method: :delete, data: { confirm: 'Are you sure?' } %>
Both options are fine, and the 2nd one with delete is the recommended one

Ruby on Rails add confirmation to link_to

I'm building a Ruby on Rails application and I have generated some scaffold.
I want to add a confirmation box (like the one that pops up when you delete an element) to a custom link I created.
The syntax for the element deletion is the following:
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
This is my custom link:
<td><%= link_to 'set done', :controller => 'users', :action => 'set_donation_done', :user => user, data: { confirm: 'Are you sure?' } %><td>
The problem is that the confirmation box on my custom link does not appear. If I inspect the element I see the following html code:
set done
What am I doing wrong?
Try with this:
<%= link_to 'set done', { controller: 'users', action: 'set_donation_done' }, user: user, data: { confirm: 'Are you sure?' } %>
Remember:
link_to(body, url_hash, html_hash)
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
Found the solution:
<td><%= link_to 'set done', {:controller => 'users', :action => 'set_donation_done', :user => user}, data: { confirm: 'Are you sure?'} %><td>
Whit this formula I have my user data in params and I can see the confirmation box!
You don't need the controller action hash. Simply show when the HTML hash starts like so:
<td><%= link_to 'Destroy', user, method: :delete,{ data: { confirm: 'Are you sure?' }} %></td>

How do you pass multiple arguments to nested route paths in Rails?

I am new to Rails and normally set up a link_to helper for a normal unnested route like so:
link_to "Delete", article_path(article.id), method: :delete, data: {confirm: "Are you sure?"}
However I am learning about nested routes and it seems I need to provide the path with two arguments for example:
link_to "(Delete)", article_comments_path(comment.article_id, comment.id), method: :delete, data:{comfirm: 'Are you sure?'}
However this does not seem to work. I have seen that you can format the link_to like this:
link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' }
But this seems confusing to me as there is no path defined and the values necessary for the path arn't directly specified either.
Is it possible to format a nested link_to path like the unnested one above or does it need to be formatted as shown in the third example? If so could someone try to explain how this array format is translated into a url for Rails to execute?
Thanks
Route:
article_comment_path - DELETE - /articles/:article_id/comments/:id(.:format) - comments#destroy
I think your route would be something like articles/:article_id/comments/:id so you can do:
<%= link_to "Delete", article_comments_path(article_id: comment.article_id, id: comment.id), method: :delete, data:{comfirm: 'Are you sure?'} %>
And your 3rd link should be
<%= link_to 'Destroy Comment', polymorphic_path(comment.article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
For details check Polymorphic routes
Update
You just need to pass locals to your partial like:
<%= render "comment", locals: {article: comment.article, comment: comment} %>
and then use
<%= link_to "Delete", article_comments_path(article.id,comment.id), method: :delete, data:{comfirm: 'Are you sure?'}%>

Resources