:confirm in rails not working

jamesdlivesinatree picture jamesdlivesinatree · Apr 4, 2013 · Viewed 13.4k times · Source

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!

Answer

craigq picture craigq · Dec 10, 2014

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?' }