Implement "Clear form"

226 views
Skip to first unread message

djlewis

unread,
May 11, 2008, 3:10:09 PM5/11/08
to Ruby on Rails: Talk
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.

Frederick Cheung

unread,
May 11, 2008, 3:37:44 PM5/11/08
to rubyonra...@googlegroups.com

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 ( <input type="reset" value="Reset!">)

Fred

djlewis

unread,
May 11, 2008, 5:45:39 PM5/11/08
to Ruby on Rails: Talk
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 Cheung <frederick.che...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages