Created
November 11, 2008 19:20
-
-
Save zhhz/23941 to your computer and use it in GitHub Desktop.
link_to_action with confirmation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a link to an action. A confirm box will pop up before | |
# you can process to action specified here. | |
# link_to_action "Delete", "Are you sure?", :method => 'delete', | |
# :class => "negative", :controller => 'price_letters', | |
# :action => 'destroy', :id => letter.id | |
# link_to_action "Effect Now", "Take effect now, and can not be reversed?", | |
# :controller => "price_letters", :action => 'effect_now', :id => letter.id | |
def link_to_action(link_text, msg = "Are you sure", attributes = {}) | |
content = (msg || "Are you sure?") | |
attrs = { :method => 'post' } | |
attrs = attrs.merge(attributes) | |
%{<a href="/#{attrs[:controller]}/#{attrs[:action]}/#{attrs[:id]}" | |
onclick="if (confirm('#{content}')) | |
{ | |
var f = document.createElement('form'); | |
f.style.display = 'none'; | |
this.parentNode.appendChild(f); | |
f.method = 'POST'; | |
f.action = this.href; | |
var m = document.createElement('input'); | |
m.setAttribute('type', 'hidden'); | |
m.setAttribute('name', '_method'); | |
m.setAttribute('value', '#{attrs[:method]}'); | |
f.appendChild(m);f.submit(); | |
} | |
return false;" class="#{attrs[:class]}">#{link_text}</a>} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment