Topic: Reseting a form..

I've got a hidden div with a form in it (which works) and after I submit a comment with the form (which also works), I perform a slide up ajax effect with RJS (that works too).  My problem is that if I immediately add another comment, my comment form is pre-filled out with my previous comment... how does one reset the form?

Here's the form:

<div id ="comment_form" style="display:none" >
<% form_remote_tag(:url => {:controller => 'comments', :action => 'create', :id => }) do %>
    <label for="comment_body"></label>
    <%= text_area :comment, :comment_body %>
      <%= submit_tag "Add Comment"  %>

<%= end_form_tag %>
<% end %>

I tried adding page['comment_form'].reset to my create.rjs but it gives me an error that it's not a function..  Any help appreciated!

Re: Reseting a form..

Two options

1) Use RJS to reset each item in the form

render :update do |page|
  page['comment_field_id'] = ''

2) Instead of putting the form on the page when the page is loaded, you can use link_to_remote and load the form in to the hidden div when clicking the "open" link.

Kyle Peyton - Web Developer
Santa Clara, CA - Startontop

Re: Reseting a form..

I'll admit to not being much of an RJS expert by any means... I put that code snippet into my RJS and, while it didn't give an error, the same problem persists.  I also tried it with "comment_form" and "comment_form_id" but no dice.  Did I miss something? 


Re: Reseting a form..

You need to explicitly set the id on the comment field so that the RJS has an ID to target and update.

<%= text_area :comment, :comment_body, :id => 'comment_id' %>

And I think the proper code for the RJS file would actually be:

page['comment_id'].replace_html ''

Kyle Peyton - Web Developer
Santa Clara, CA - Startontop

Re: Reseting a form..

No you don't.

When you use the form helper it will automatically specify a name AND an ID for that field. For example, if I did

text.field 'blog' 'subject'

the ID would be 'blog_subject'

So, for your RJS, vin, it would be

 page['comment_comment_body'] = ''

Try that out.

I would also recommend changing the "comment_body" field to just be "body", as having comment in there is needless repetition.

Last edited by Radar (2007-08-01 21:44:57)

Re: Reseting a form..

I tried a bunch of stuff (before seeing your post Radar) and settled on this:

<% form_remote_tag(:url => {:controller => 'comments', :action => 'create', :id => }, :html => {:name => 'comment_id'}, :before => "javascript:document.comment_id.reset();") do %>

So far that seems to be working.. javascript is a bugger!

Anyway, thanks for all your help!