17
votes

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!

5

5 Answers

62
votes

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

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
5
votes

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.

4
votes

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

0
votes

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