Using Ruby on Rails’ View Helpers To Create a Javascript Form Reset Button
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 %>
Trackbacks
Use this link to trackback from your own site.







Seriously, who uses those reset buttons? Aren’t those just the bastards you sometimes accidentally push instead of Submit and then you end up rewriting everything you just already wrote? But somehow everyone makes them anyway since Tim Berners-Lee wanted it that way?
Even your blag doesn’t have one! :-P
I’m in the process of adding a huge cancel button to the front page right now.
Hey, you don’t need javascript for that.
You can generate an html-only reset button by doing this:
‘reset’ %>
But you’ll probably want to add a :name and :id parameters too:
‘reset’, :name => ‘reset’, :id => ‘reset’ %>
Because by default these will be ‘object_submit’
Regards,
Kikito
The :name and :idparameter is for t
I tried the method specified by kikito, but it gave a submit button with reset text. What I did instead was this
‘reset’, :id => ‘reset’, :type => ‘reset’ %>
This created an HTML reset button for me.
Thanks for the solutions it’s work fine