rails 3, how add a simple confirmation dialog when user clicks a link

霸气de小男生 提交于 2019-12-10 03:09:24

问题


what is the easiest way in rails 3 to to ask "Are you sure you want to do that?" when the user clicks on a link?

the link in this case overwrites some data with a new value. It's a simple link_to (GET) back to the same controller method, with a param[] added.


回答1:


EDIT:
<%= link_to "Do something", {:controller => "foo", :action => "bar"}, :confirm => "Are you sure you want to do that?" %>




回答2:


For those like me that came here but are on Rails 4/5:

link_to "Title", some_path, data: { confirm: "Are you sure?" }

The documentation




回答3:


There is a :confirm option:

link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?"
# => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?"">Visit Other Site</a>

You should have unobtrusive JavaScript driver like this one for JQuery. It's pretty simple to setup.

Details are in Rails API. For more details about UJS in Rails 3 you can follow this tutorial.



来源:https://stackoverflow.com/questions/5528641/rails-3-how-add-a-simple-confirmation-dialog-when-user-clicks-a-link

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