# Implement "Clear form"

**URL:** <https://www.ruby-forum.com/t/implement-clear-form/137309>\
**Category:** Rails\
**Created:** [May 11, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/implement-clear-form/137309 "2008-05-11T21:11:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djlewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@djlewis](https://www.ruby-forum.com/u/djlewis)\
**Post date:** [May 11, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/implement-clear-form/137309/1 "2008-05-11T21:11:05Z")

</div>

What’s a good way to clear all the fields a form that can apply  
generally to any form (so I can write one method and apply it to any  
one of a set of forms). If that can’t be done with a form (say  
because I can’t know programmatically what field are in a form) I’d be  
content to simply clear all the attributes in a record and ship that  
to the form.

Basically, I’m trying to implement a “Clear all data” button for a  
bunch of pages.

Thanks. --David.

---

<div class="post-metadata">

**Author:** ![djlewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@djlewis](https://www.ruby-forum.com/u/djlewis)\
**Post date:** [May 11, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/implement-clear-form/137309/2 "2008-05-11T21:38:40Z")

</div>

On 11 May 2008, at 20:10, djlewis wrote:

> What’s a good way to clear all the fields a form that can apply  
> generally to any form (so I can write one method and apply it to any  
> one of a set of forms). If that can’t be done with a form (say  
> because I can’t know programmatically what field are in a form) I’d be  
> content to simply clear all the attributes in a record and ship that  
> to the form.

With javascript you can call the form’s reset method or you can have a  
reset button ( )

Fred

---

<div class="post-metadata">

**Author:** ![djlewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@djlewis](https://www.ruby-forum.com/u/djlewis)\
**Post date:** [May 11, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/implement-clear-form/137309/3 "2008-05-11T23:46:09Z")

</div>

I just realized that, in response to the “clear” button submit, simply  
issuing “render” in the controller clears all the fields, as long as  
there is no @record\_instance present. Is that legit – any downsides?

(Not long ago, I was trying to evade this behavior. Now it’s the  
solution to a problem!)

Another way I though of is to go through params[:record\_instance] for  
the names of the most recently submitted form’s fields, set each those  
to blank and then render. Didn’t get to test that yet, however, since  
having no @record\_instance at all seems to do the trick.

–David

On May 11, 3:37 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
