18

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!

lulalala
  • 17,572
  • 15
  • 110
  • 169
jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36

7 Answers7

62

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?' }
craigq
  • 692
  • 6
  • 7
12

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
Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • I have both of the tags you're referring to, but it still does not pop up the confirm box – jamesdlivesinatree Apr 04 '13 at 06:31
  • I suppose you `bundle install`and restarted your rails server? – Benjamin Bouchet Apr 04 '13 at 06:50
  • how can I test if javascript is working? I don't think it is, and I think this is the problem. I'm just not sure what's causing it. – jamesdlivesinatree Apr 04 '13 at 07:20
  • Yes, I have run bundle install and restarted server. No luck. I have also noticed that nothing comes up in the console when debugging with chrome and clicking the button that's supposed to pop up the :confirm. Also, my gemfile.lock displays jquery-rails (2.2.1). Is there something else I may be missing? – jamesdlivesinatree Apr 04 '13 at 07:54
  • Also, when I run view source on the page, this is the HTML I get for that button. – jamesdlivesinatree Apr 04 '13 at 08:13
  • Do you have the javascript_include_tag in your template? – Fred Apr 04 '13 at 12:44
  • @Fred this is in my template: <%= javascript_include_tag "application" %> – jamesdlivesinatree Apr 04 '13 at 21:45
  • Sounds good. At this point, I'd use some browser based debugger like Firebug to examine the page after it loaded and see what jQuery did to the link element. – Fred Apr 04 '13 at 22:08
  • @Fred please see revised question with added code from Chrome's inspector. Everything looks like it's added fine. jquery.js and jquery-ujs.js are in the header, and I am receiving no bugs in the console when rendering the page. I'm not sure where to go from here and why this still isn't working. – jamesdlivesinatree Apr 05 '13 at 01:12
  • And when I said header, I meant the tag. – jamesdlivesinatree Apr 05 '13 at 01:21
6

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.

Fred
  • 8,582
  • 1
  • 21
  • 27
  • 1
    Thank you for your explanation. Very informative and makes a lot more sense now. However, I am still curious on how to get the :confirm to pop up. This is still not working for me. – jamesdlivesinatree Apr 04 '13 at 06:33
4

Feel pretty dumb, but adblock was blocking the message box. Sorry about that. All is well now, I just disabled adblock.

jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36
2

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>
user16217248
  • 3,119
  • 19
  • 19
  • 37
Pratham N
  • 31
  • 2
1

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.

installero
  • 9,096
  • 3
  • 39
  • 43
0

I have a pop-up blocker running in Chrome. I just whitelisted http://localhost:3000 and it worked for me.

Cruz Nunez
  • 2,949
  • 1
  • 23
  • 33