Reputation: 730
I just started coding in ruby on rails and I've been following a guide which is using a more outdated version of rails than I am using. I am using 3.2.12
This is my code:
<%= button_to 'Destroy', product, :method => "delete", :confirm => 'Are you sure?' %>
From what I understand, these are symbols that are passed to rails, which is then converted to either an html or javascript action that then pops up the message box and deletes the object if applicable. The above code destroys the object, but it does not pop up the confirm box. Why is this? Also, I had the above as the following at first:
<%= link_to 'Destroy', product, :method => "delete", :confirm => 'Are you sure?' %>
The confirm box is not popping up under any circumstance, using link_to or button_to. Below is the html rendered when inspected using Chrome's inspector. jquery and jquery-ujs are loaded into the as well, so I'm not sure where to go from here.
<input name="_method" type="hidden" value="delete">
<input data-confirm="Are you sureeee?" type="submit" value="Destroy">
<input name="authenticity_token" type="hidden" value="Q2xicqELHYHtrwarbtPBe5PT2bZgWV5C+JdcReJI8ig=">
Thanks!
Upvotes: 18
Views: 18013
Reputation: 41
In Rails 7.0.4.3 it got changed and you can now use link using turbo-method
<%= link_to 'Delete', article_path(article), data: { "turbo-method": :delete, 'turbo-confirm': "Are you sure" } %></td>
Upvotes: 3
Reputation: 9806
If you want to delete something with confirmation box in rails 7, you may try this one:
With button_to
(more prefered IMHO):
<%= button_to 'Destroy', product, method: :delete,
form: {data: {turbo_confirm: 'Are you sure?'}} %>
This will render an HTML form tag which sends a POST
request on submit (after confirmation) but with a hidden _method
attribute with 'delete' value. So that rails will treat this request as if it has a DELETE
method.
It will be routed to products#destroy
(or whatever your routes are saying).
With link_to
:
<%= link_to 'Destroy', product,
data: {turbo_method: :delete, turbo_confirm: 'Sure?'} %>
This will render a simple a
tag with data-turbo-method
and data-turbo-confirm
attributes. Clicking this link will trigger a confirmation box and if "OK" is chosed, a real DELETE
request will be sent.
If you want to end destroy
action in your controller with a redirect_to
, some browsers will redirect to a new location with DELETE
method (causing errors), so make sure to add status: :see_other
parameter, like the guides suggests.
Upvotes: 1
Reputation: 3149
I have a pop-up blocker running in Chrome. I just whitelisted http://localhost:3000
and it worked for me.
Upvotes: 0
Reputation: 792
I had to add my confirm attribute inside the data attribute to get it to work. I am using rails 4 with bootstrap. I hope this helps someone else who has that issue.
link_to 'Delete', @rule, method: :delete, data: { confirm: 'Are you sure you want to delete this alert?' }
Upvotes: 62
Reputation: 730
Feel pretty dumb, but adblock was blocking the message box. Sorry about that. All is well now, I just disabled adblock.
Upvotes: 4
Reputation: 8602
The difference between link_to and button_to is the HTTP verb. link_to issues GET requests and button_to issues POST requests. With the RESTful routing, the delete
action is a POST request to controller/id. If you issue a GET to controller/id, it is dispatched to the show
action.
AIUI, link_to with anything other than the GET verb is a Bad Idea. One, right-clicks don't preserve the verb. Two, you don't want bots crawling the page to follow the link and thereby trigger the delete
action even though you probably need to be logged in to actually modify the database.
Upvotes: 6
Reputation: 13181
This relies on jQuery, ensure you have the following:
in your Gemfile
group :assets do
gem 'jquery-rails'
end
in your assets/javascripts/application.js file, before the line //= require_tree .
//= require jquery
//= require jquery_ujs
Upvotes: 12