-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make disable_with default in submit_tag #21135
Conversation
# | ||
def submit_tag(value = "Save changes", options = {}) | ||
options = options.stringify_keys | ||
|
||
tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options) | ||
tag :input, { "data" => {"disable_with": "Please wait..."}, "type" => "submit", "name" => "commit", "value" => value }.update(options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a space after {
and before }
, like the outer hash has.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also we've just stringified the keys above. So we need to use a string key here as well.
@kaspth updated with your style changes sir :) |
@@ -2226,7 +2226,7 @@ def test_submit_with_object_as_new_record_and_locale_strings | |||
end | |||
|
|||
expected = whole_form('/posts', 'new_post', 'new_post') do | |||
"<input name='commit' type='submit' value='Create Post' />" | |||
"<input name='commit' data-disable-with='Please wait...' type='submit' value='Create Post' />" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This indentation is wrong now
thank you for the work. It makes sense but I think we may have problems with with the hash update. Could you add tests that it is working with the inputs that I pointed? |
Ok yeah, now that I fixed the tests in |
ah I see that passing "data-disable-with" to the options has will also result in problems, writing a test and working on a solution. |
Alright I've updated the code and written some tests to ensure that a user specified option is not overridden. |
|
||
tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options) | ||
tag :input, tag_options |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
submit_tag
has changed enough that I think we need a benchmark to make sure there isn't any serious performance regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I appreciate the feedback, let me take some time tonight solidifying and benchmarking everything.
@kaspth So I've done some benchmarking and applied a fix. I found that After changing it to expect the symbol key for the nested hash (which ruby does anyway so it's not necessary to deep stringify) the performance normalized back to 80k ip/s. # Rails#master
Calculating -------------------------------------
submit_tag old 7.988k i/100ms
-------------------------------------------------
submit_tag old 83.042k (± 2.8%) i/s - 415.376k
# Rails with updated submit_tag code
Calculating -------------------------------------
submit_tag new 7.711k i/100ms
-------------------------------------------------
submit_tag new 80.468k (± 2.2%) i/s - 408.683k I think the last thing i'm trying to wrap my head around is that nested hashes by default right now are being stringified only on the base level, and not on the nested level. e.g. irb(main):004:0> { "data" => { "disable_with": "Please wait..." } }.stringify_keys
=> {"data"=>{:disable_with=>"Please wait..."}}
irb(main):007:0> {}.update({ "data" => { "disable_with": "Please wait..." } }.stringify_keys)
=> {"data"=>{:disable_with=>"Please wait..."}} This would mean this is happening for other data attributes being passed in already as well. Am i missing something here? |
|
It doesn't. If you need i18n you can just explicitly set |
If that is the case they can just override the default. |
A problem that I think is: is it possible to remove |
Maybe using "Saving..." instead of "Please wait..." would be a little more device friendly? |
Based on this implementation, no it doesn't look like there's a way to opt-out. Adding |
Alright, I'll add this functionality :) |
No I18n makes the feature only available for en apps. Others will be forced to specify something everywhere in a project. Also I18n makes it possible to disable the feature and make upgrading easier. |
We don't have I18n support right now and I want to keep it out. I18n by default will slow down a lot submit_tag for applications that only use english (I bet it is something like 80% of the apps) |
@rafaelfranca so what should I do after upgrading? Go through my app and specify Did you include multi-language apps into those 80%? They would also experience problems. What about people that don't want a change? How would they disable feature globally without modifying entire app? I think 80% is overestimated: exclude people that don't want this feature and multi-language apps (<- most successful ones by the way). It looks bad without ability to globally disable. We should have a way of doing it. Also we need somehow tell people in the internationalisation guide:
I hardly image it will work without I18n. It will be top1 change request. |
Agree, good point.. I prefer to have it disabled globally without using i18n, but I don't want to introduce other global switch just for that, so I agree that I18n would solve this and also enable other features. 👍 |
What about using the button text and just disabling the button? If you need something specific, you can override with a string or i18n otherwise the button is just disabled? Not enough user feedback? |
Since our submit button defaults to "Save Changes" e.g def submit_tag(value = "Save changes", options = {}) It would seem like it sense to default the text to "Saving..." and allow the user to mess with i18n or define there own disable_with as needed. Will have some i18n changes coming through soon. |
I like @derekprior's idea. We should not aim to make a good interface - there is no consensus on what label is the best. If we just disable the button, we fix the technical issue that all of agree that needs to be addressed. |
Don't get me wrong - having a conventional place to go to override the That's in line with what users see today except that the button is On Friday, August 7, 2015, Bogdan Gusiev notifications@github.com wrote:
|
Makes sense, in that case we don't want the i18n anymore since it will just be using the value of the submit tag anyway? |
Alright the feature can now be disabled on a per tag basis and uses the The only thing missing now is if/how we want to be able to disable this feature globally. |
What do you guys think about adding a globally configurable action_view settings like so? # Specify whether submit_tag should automatically disable on click
cattr_accessor :automatically_disable_submit_tag
@@automatically_disable_submit_tag = true So then users that want to disable the feature for whatever reason can set the preference in application.rb config.action_view.automatically_disable_submit_tag = false |
I'm 👍 for adding a config to opt out, if only to aid migration. On Fri, Aug 7, 2015, 8:26 PM Justin notifications@github.com wrote:
|
Added opt out settings + a test for it. |
Do I need to do anything further for this? |
# 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 <tt>:data => { :disable_with => false }</tt> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New hash syntax
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`
Ok so in summary of recent changes:
|
Any other ideas guys? |
make disable_with default in submit_tag
Can you add this issue number to the upgrade guide meta issue? |
@sgrif Where can I find that? |
Just did it. Was on my phone earlier and didn't have time to search. |
Ah thankyou I kind of had no idea what you were talking about since this is my first time contributing to rails :P but thanks :D |
Should be able to remove when upgrading to Rails 5 thanks to rails/rails#21135
It looks like this does not work with the <form class="button_to" method="post" action="…">
<button type="submit">…</button>
</form> @DropsOfSerenity @sgrif I think we should make that happen… what do you think? |
I heard on the "Bike Shed" podcast that the idea of possibly making disable_with a default for submit_tag to prevent double submission by default, thought I would draft up a pull request for it and take a stab at it.