Forum: Ruby on Rails clearing out submitted form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Da734ede01dc6e5bace38f5fbc0fc4f6?d=identicon&s=25 Kevin Tambascio (ktambascio)
on 2007-02-27 14:04
I'm trying to put together a basic blog application for fun, and I'm
using an RJS template to insert a new comment into existing comments.
The comment data is inserted, but the comment form at the bottom of the
page still contains the comment.  I tried to clear out the text from the
form this way:

page.insert_html :top, 'comments', :partial => 'show_comment'
page.replace_html 'blog_entry_comment_body', ''
page.replace_html 'blog_entry_comment_subject', ''

But this doesn't work either.  Any thoughts on how to clear out the form
contents after the comment is added?  The form looks like this:

<% form_remote_tag(:url => {:action => 'ajax_add_comment', :controller
=> 'Comment', :id => @blog_entry.id }) do %>

    <%= render :partial=>"comment/form" %>
    <%= submit_tag 'Add Comment' %>
<% end %>

Firefox may also be putting the data in the form, as part of it's
auto-completion functionality, I suppose.  Same behavior is present in
IE7.

Thanks in advance,
Kevin
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-02-27 15:28
(Received via mailing list)
> page.replace_html 'blog_entry_comment_body', ''
> page.replace_html 'blog_entry_comment_subject', ''

this isn't doing what you think it is doing.  it replaces the contents
of tags, not the values. (actually, replace_html will work on a
textarea, but will not on type="text" input)

you want to do something like:

# blog_entry_comment_body => textarea
page << "$('blog_entry_comment_body').value = ''"
# also works
#page.replace_html('blog_entry_comment_body', '')

# blog_entry_comment_subject => input type="text"
page << "$('blog_entry_comment_subject').value = ''"

you are submitting the form via ajax, so therefore the page is not
being 'reloaded' and thus firefox or IE is not auto-filing the forms
for you at this point because you never left the page.
B28d0b9ff630f071d8867aa164f27a80?d=identicon&s=25 Hamza Khan-cheema (hamza)
on 2007-02-28 15:58
(Received via mailing list)
You can also use Collection Proxies in RJS  like so :

page.select('div textarea').each do |element|
  element.value=""
end

This takes all of the text areas in a div and replaces the value.  You
could also use the div name like so :

page.select('#id_of_textarea_here').each do |element|
  element.value=""
end

So if I had a <textarea id="id_of_textarea_here" ...

That should replace the value.

Hamza
This topic is locked and can not be replied to.