module ActionView #:nodoc: module Helpers #:nodoc: module UrlHelper #:nodoc: private def convert_options_to_javascript!(html_options) confirm, popup = html_options.delete("confirm"), html_options.delete("popup") # post is deprecated, but if its specified and method is not, assume that method = :post method, post = html_options.delete("method"), html_options.delete("post") if !method && post ActiveSupport::Deprecation.warn( "Passing :post as a link modifier is deprecated. " + "Use :method => \"post\" instead. :post will be removed in Rails 2.0." ) method = :post end rest_url = html_options.delete("rest_url") html_options["onclick"] = case when popup && method raise ActionView::ActionViewError, "You can't use :popup and :post in the same link" when confirm && popup "if (#{confirm_javascript_function(confirm)}) { #{popup_javascript_function(popup)} };return false;" when confirm && method "if (#{confirm_javascript_function(confirm)}) { #{method_javascript_function(method, rest_url)} };return false;" when confirm "return #{confirm_javascript_function(confirm)};" when method "#{method_javascript_function(method, rest_url)}return false;" when popup popup_javascript_function(popup) + 'return false;' else html_options["onclick"] end end def method_javascript_function(method, url = nil) submit_function = "var f = document.createElement('form'); f.style.display = 'none'; " + "this.parentNode.appendChild(f); f.method = 'POST'; f.action = #{url.nil? ? "this.href" : "'#{url}'"};" unless method == :post submit_function << "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); " submit_function << "m.setAttribute('name', '_method'); m.setAttribute('value', '#{method}'); f.appendChild(m);" end submit_function << "f.submit();" end end end end