How to Disable a Form Submit Button "A Là Ruby on Rails Way"

How to disable a form submit button a là Ruby on Rails Way?

The Rails jQuery bridge (jquery_ujs.js) code actually has a helper for this.

<%= form.submit "Save", id: "button_id", class: "button", disable_with: "Submitting..."

It will replace the button text with the value you give.

See http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag

API change: as Renan suggests in a comment below, as of Rails 4 beta there is a deprecation notice on the disable_with option. It should be changed to be a data attribute:

<%= form.submit "Save", id: "button_id", class: "button", data: {disable_with: "Submitting..."} %>

This should work with all recent versions of Rails as it's what the option did anyway. So it'll be one less deprecation notice to fix when you upgrade to Rails 4. :)

Rails Ajax Form Disable Submit Button

You can hide the submit button when the request is made and show a loading image while the request is running. For example in a form_remote_tag:

<% form_remote_tag :url => "/some_url", 
:loading => "$('submit_span').toggle(); $('loading_span').toggle();",
:complete => "$('submit_span').toggle(); $('loading_span').toggle();" do %>

-- FORM CONTENTS --

<span id="submit_span">
<%= submit_tag "Submit" %>
</span>
<span id = "loading_span" style = "display: none;">
<%= image_tag("loading.gif")%>
</span>
<% end %>

When the request completes (Success OR Failure) you unhide the submit and hide the loading span.

More docs on the various callbacks:

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M002174

In rails controllers, how to prevent double submit (when user double-clic submit button or hit enter twice)?

Try using Redis locking and surround your block with something like

Redis.current.lock("#{current_user.id}.action_name") do
# Some code
end

This is the gem I'm using
https://github.com/mlanett/redis-lock

Form Submit button only works after reload

I believe this is an HTML issue, not a Rails issue. Per this discussion Form inside a table, <form> can not be placed inside <table> or <tbody> or <tr>. After moving the <form> to wrap the table and putting the controls inside the respective <td> the form works.

I still don't understand why refreshing the page made the form work, but...

How to disable submit button once it has been clicked?

Probably you're submitting the form twice.
Remove the this.form.submit() or add return false at the end.

you should end up with onClick="this.disabled=true; this.value='Sending…';"

Add a CSS class to %= f.submit %

<%= f.submit 'name of button here', :class => 'submit_class_name_here' %>

This should do. If you're getting an error, chances are that you're not supplying the name.

Alternatively, you can style the button without a class:

form#form_id_here input[type=submit]

Try that, as well.

Ruby on Rails - Suppress an error message

To save without validating use:

@parent.save(:validate => false)

Also, don't forget you can create conditional validation rules if needs be. For example, add a virtual attribute (an instance variable that is not persisted to the DB) accessible via bare_bones?. Then modify a validator like so:

validates_presence_of :nickname, :unless => "bare_bones?"

Then, in your controller you would do something like this:

@parent = Parent.new params[:parent]
@parent.bare_bones = true
@parent.save()

Hope this helps.

rails seems to delete query params in URL with 'button_to' helper and 'get' method

I suggest you add params: { } at the end of button_to inside the braces that hold http_options, it will generate hidden_field which contains the value.

 params: { :delete_id => saved_search.id }



Related Topics



Leave a reply



Submit