:confirm in rails not working

无人久伴 提交于 2019-12-10 01:02:33

问题


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!


回答1:


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



回答2:


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



回答3:


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:


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




回答5:


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



来源:https://stackoverflow.com/questions/15800092/confirm-in-rails-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!