From 18605adec36f8487374d68cdb031e89e91379c56 Mon Sep 17 00:00:00 2001 From: Rizwan Reza Date: Thu, 23 Dec 2010 22:43:46 +0330 Subject: HTML5 button_tag helper This tag is similar in nature to submit_tag, but allows more control. It also doesn't submit if submit type isn't used, allowing JavaScript to control the flow where required. For more information: http://www.whatwg.org/specs/web-apps/current-work/multipage/the-button-element.html#the-button-element --- .../lib/action_view/helpers/form_tag_helper.rb | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 9500e85e8b..3daf68d29f 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -401,6 +401,56 @@ module ActionView tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys) end + # Creates a button element that defines a submit button, + # resetbutton or a generic button which can be used in + # JavaScript, for example. You can use the button tag as a regular + # submit tag but it isn't supported in legacy browsers. However, + # button tag allows richer labels such as images and emphasis. + # + # ==== Options + # * :confirm => 'question?' - If present, the + # unobtrusive JavaScript drivers will provide a prompt with + # the question specified. If the user accepts, the form is + # processed normally, otherwise no action is taken. + # * :disabled - If true, the user will not be able to + # use this input. + # * :disable_with - Value of this parameter will be + # used as the value for a disabled version of the submit + # button when the form is submitted. This feature is provided + # by the unobtrusive JavaScript driver. + # * Any other key creates standard HTML options for the tag. + # + # ==== Examples + # button_tag + # # => + # + # button_tag "Ask me!" + # # => + # + # button_tag "Checkout", :disable_with => "Please wait..." + # # => + # + def button_tag(label = "Button", options = {}) + options.stringify_keys! + + if disable_with = options.delete("disable_with") + options["data-disable-with"] = disable_with + end + + if confirm = options.delete("confirm") + options["data-confirm"] = confirm + end + + ["type", "name"].each do |option| + options[option] = "button" unless options[option] + end + + content_tag :button, label.to_s.html_safe, { "type" => options.delete("type") }.update(options) + end + # Displays an image which when clicked will submit the form. # # source is passed to AssetTagHelper#path_to_image -- cgit v1.2.3