Using Ruby on Rails’ View Helpers To Create a Javascript Form Reset Button

Posted on August 22, 2008

Apparently, it is possible reset an entire form using Javascript from a button using the Rails form helpers. Up until now, I’ve been sniffing the submit tag’s name in the controller and avoiding the save when posting an update. The beauty of the Javascript form reset method is that it requires absolutely no code in the controller.

Oddly enough, there did not exist any easily findable examples from Google. So, here is how to reset a form to the original using only Javascript.

<% form_for :person, person, :url => { :action => "update" } do |f| %>
# Other stuff
<%= f.submit "Cancel", :type => "button", :onclick => "this.form.reset()" %>
# Resets the form back to the original values set in the edit template
<% end %>