Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2012
1,656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. # Replacement for Rails' default button_to helper
  2. # using HTML button element rather than HTML input element
  3. def button_to(name, options = {}, html_options = {})
  4. html_options = html_options.stringify_keys
  5. convert_boolean_attributes!(html_options, %w( disabled ))
  6.  
  7. method_tag = ''
  8. if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
  9. method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
  10. end
  11.  
  12. form_method = method.to_s == 'get' ? 'get' : 'post'
  13.  
  14. request_token_tag = ''
  15. if form_method == 'post' && protect_against_forgery?
  16. request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
  17. end
  18.  
  19. if confirm = html_options.delete("confirm")
  20. html_options["onclick"] = "return #{confirm_javascript_function(confirm)};"
  21. end
  22.  
  23. url = options.is_a?(String) ? options : self.url_for(options)
  24. name ||= url
  25.  
  26. html_options.merge!("type" => "submit", "value" => name)
  27.  
  28. "<form method=\"#{form_method}\" action=\"#{escape_once url}\" class=\"button-to\">" +
  29. method_tag + content_tag("button", name, html_options) + request_token_tag + "</form>"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement