From 3822a322a82a19a9341a21a0cb1e36653da09c46 Mon Sep 17 00:00:00 2001 From: Justin Schiff Date: Wed, 5 Aug 2015 09:38:43 -0700 Subject: Make disable_with default in submit_tag Prevents double submission by making disable_with the default. Default disable_with option will only be applied if user has not specified her/his own disable_with option, whether that is in the `data-disable-with` string form or the `:data => { :disable_with => "Saving..." }` hash form. disable_with will default to the value attribute. A configuration option was added to opt out of this functionality if the user so desires. `config.action_view.automatically_disable_submit_tag = false` --- .../lib/action_view/helpers/form_tag_helper.rb | 32 ++++++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'actionview/lib/action_view/helpers') diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index d36701955a..af684e05c6 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -414,34 +414,48 @@ module ActionView # the form is processed normally, otherwise no action is taken. # * :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. + # provided by the unobtrusive JavaScript driver. To disable this feature for a single submit tag + # pass :data => { disable_with: false } Defaults to value attribute. # # ==== Examples # submit_tag - # # => + # # => # # submit_tag "Edit this article" - # # => + # # => # # submit_tag "Save edits", disabled: true - # # => + # # => # - # submit_tag "Complete sale", data: { disable_with: "Please wait..." } - # # => + # submit_tag "Complete sale", data: { disable_with: "Submitting..." } + # # => # # submit_tag nil, class: "form_submit" # # => # # submit_tag "Edit", class: "edit_button" - # # => + # # => # # submit_tag "Save", data: { confirm: "Are you sure?" } - # # => + # # => # def submit_tag(value = "Save changes", options = {}) options = options.stringify_keys + tag_options = { "type" => "submit", "name" => "commit", "value" => value }.update(options) + + if ActionView::Base.automatically_disable_submit_tag + unless tag_options["data-disable-with"] == false || (tag_options["data"] && tag_options["data"][:disable_with] == false) + disable_with_text = tag_options["data-disable-with"] + disable_with_text ||= tag_options["data"][:disable_with] if tag_options["data"] + disable_with_text ||= value.clone + tag_options.deep_merge!("data" => { "disable_with" => disable_with_text }) + else + tag_options.delete("data-disable-with") + tag_options["data"].delete(:disable_with) if tag_options["data"] + end + end - tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options) + tag :input, tag_options end # Creates a button element that defines a submit button, -- cgit v1.2.3