:disable_with
Jeremy •
Gotchas, Rails, Tips •
About a week ago I stumbled across an option that you can pass to the submit_tag form helper in Rails — :disable_with.
- <%= sumbit_tag "Save", :disable_with => "Saving..." %>
Now (thanks to some automatically injected javascript) when you press this "Save" button it will disable itself, change its text to "Saving...", and then submit the form. Cool, right?
Yeah, most of the time it's cool. It's nice to do anything that gives the user an indication of what's going on — especially in a way that prevents them from repeatedly pushing a button on a form that takes a second or two longer than it should.
However, there is one time it's not cool! It's not cool if you have a form that contains more than one submit button and you expect to do different things depending on which button is pushed. A button is simply an input tag just like any other input tag in a form. It has a type, "submit" in this case, a name and a value. The GOTCHA is that, like any other input field, if it's disabled it doesn't get posted to the server. Oooops! So much for using :disable_with on this particular form!
