# RJS adds comment but doesn't update form

**URL:** <https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470>\
**Category:** Rails\
**Created:** [April 11, 2006, 9:42am UTC](https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470 "2006-04-11T09:42:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2006, 9:42am UTC](https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470/1 "2006-04-11T09:42:01Z")

</div>

Hi,

When a user adds a comment i want to add the comment to the end of the  
comments list using ajax so there is no full page refresh. The code i  
have below adds the comment to the database but doesn’t update the  
comments div with the new comment.

Can anyone help? This is my first use of rjs templates and i have read  
[http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates](http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates)

I am using eclipse with rad rails on windows for development and i am  
running rails 1.1.

post.rhtml  
\<%= render :partial =\> “post”, :object =\> @post %\>

* * *

\<%= render :partial =\> "comment", :collection =\> @post.comments %\>

\<p\<%= render :partial =\> ‘newcomment’, :id =\> @post %\>

\_comment.rhtml

\<%= comment.name %\> Says:

\<%= comment.comment %\>

\<%= comment.created\_at.to\_s(:long) %\>

* * *

blog\_controller

def comment  
Post.find(params[:id]).comments.create(params[:comment])  
render :update do |page|  
# show the new comment at the top of the comments display  
page.insert\_html(:top, “comments”, :partial =\> “comment”)  
# quick and dirty way to empty the form that was just submitted  
page.replace\_html(“commentform”, :partial =\> “commentform”)  
end

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2006, 9:46am UTC](https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470/2 "2006-04-11T09:46:48Z")

</div>

John B. wrote:

> Hi,
> 
> When a user adds a comment i want to add the comment to the end of the  
> comments list using ajax so there is no full page refresh. The code i  
> have below adds the comment to the database but doesn’t update the  
> comments div with the new comment.
> 
> Can anyone help? This is my first use of rjs templates and i have read  
> [http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates](http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates)
> 
> I am using eclipse with rad rails on windows for development and i am  
> running rails 1.1.
> 
> post.rhtml  
> \<%= render :partial =\> “post”, :object =\> @post %\>
> 
> * * *
> 
> \<%= render :partial =\> "comment", :collection =\> @post.comments %\>
> 
> \<p\<%= render :partial =\> ‘newcomment’, :id =\> @post %\>
> 
> \_comment.rhtml
> 
> \<%= comment.name %\> Says:
> 
> \<%= comment.comment %\>
> 
> \<%= comment.created\_at.to\_s(:long) %\>
> 
> * * *
> 
> blog\_controller
> 
> def comment  
> Post.find(params[:id]).comments.create(params[:comment])  
> render :update do |page|  
> # show the new comment at the top of the comments display  
> page.insert\_html(:top, “comments”, :partial =\> “comment”)  
> # quick and dirty way to empty the form that was just submitted  
> page.replace\_html(“commentform”, :partial =\> “commentform”)  
> end

You’re lacking an “end” to your render :update call.

Indent the code under render :update and it will be easier to see, but  
you call render :update do |page| without an end statement.

I’ve been finding RJS tricky to debug since it fails so silently.

Jeff

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2006, 9:52am UTC](https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470/3 "2006-04-11T09:52:04Z")

</div>

Jeff C.man wrote:

> John B. wrote:

> > blog\_controller
> > 
> > def comment  
> > Post.find(params[:id]).comments.create(params[:comment])  
> > render :update do |page|  
> > # show the new comment at the top of the comments display  
> > page.insert\_html(:top, “comments”, :partial =\> “comment”)  
> > # quick and dirty way to empty the form that was just submitted  
> > page.replace\_html(“commentform”, :partial =\> “commentform”)  
> > end

def comment  
Post.find(params[:id]).comments.create(params[:comment])  
render :update do |page|  
# show the new comment at the top of the comments display  
page.insert\_html(:top, “comments”, :partial =\> “comment”)  
# quick and dirty way to empty the form that was just submitted  
page.replace\_html(“commentform”, :partial =\> “commentform”)  
end  
end

This should do it.

If you still have troubles, one thing to try is to simplify your RJS  
calls to eliminate potential problems in your partials or with your  
syntax. For example, to try a simple

page.replace\_html “This is a test”

in place of a partial call that doesn’t seem to be working. If you see  
“This is a test”, then your syntax is working and you can narrow it down  
to a possible error in the partial.

Hope this helps,

Jeff

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2006, 1:50pm UTC](https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470/4 "2006-04-11T13:50:44Z")

</div>

then you have a problem somewhere else. the ‘end’ closes the ‘render  
:update’ block

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2006, 11:33am UTC](https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470/5 "2006-04-11T11:33:06Z")

</div>

I followed your advice with the page.replace\_html “This is a test” and  
it turned out there was a problem with the partial.

I have used the following code below to render the post and comments  
separetly but i will refactor this to make it add the comment to the end  
of the comment list and update the no of comments on the end of the post  
improving performane.

The extra end you suggested causes a syntax error but removing it  
works?? Seems strange.

def comment  
Post.find(params[:id]).comments.create(params[:comment])  
@post = Post.find(params[:id])  
render :update do |page|  
page.replace\_html( “postdiv”, :partial =\> “post”, :id =\> @post)  
page.replace\_html(“commentlistdiv”, :partial =\> “commentlist”,  
:object =\> @post)  
end

Thanks

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/rjs-adds-comment-but-doesnt-update-form/57470/6 "2006-04-11T18:30:51Z")

</div>

John B. wrote:

> I followed your advice with the page.replace\_html “This is a test” and  
> it turned out there was a problem with the partial.
> 
> I have used the following code below to render the post and comments  
> separetly but i will refactor this to make it add the comment to the end  
> of the comment list and update the no of comments on the end of the post  
> improving performane.
> 
> The extra end you suggested causes a syntax error but removing it  
> works?? Seems strange.
> 
> def comment  
> Post.find(params[:id]).comments.create(params[:comment])  
> @post = Post.find(params[:id])  
> render :update do |page|  
> page.replace\_html( “postdiv”, :partial =\> “post”, :id =\> @post)  
> page.replace\_html(“commentlistdiv”, :partial =\> “commentlist”,  
> :object =\> @post)  
> end
> 
> Thanks

I don’t know what’s going on in your code, but the snippet you posted is  
missing an “end” for its “do”.

The “render :update” command takes a block as its parameter, and a block  
is formed by “do … end”.

What you have there is:

def comment  
…  
render :update do |page|  
…  
end

See the problem?

You need

def comment  
…  
render :update \<block\_goes\_here\>  
end

like so:

def comment  
…  
render :update do |page|  
…  
end  
end

If what you posted is working, then you have an extra “end” somewhere  
else or there’s some other kind of problem with your syntax.

If you’re having a problem with a partial, what I do is to go into the  
partial and make it as simple as possible, start with something like

\_post.rhtml

This is \_post.rhtml

and then try it. If it you see “This is \_post.rhtml”, then try adding  
an instance variable:

controller:  
def comment  
@post = “This is a post”  
render :update do |page|  
page.replace\_html “postdiv”, :partial =\> “post”  
end  
end

\_post.rhtml:

\<%= @post %\>

and so on. Then when you see “This is a post”, you know that the  
variable is making it into your partial, and you can replace it with the  
proper assignment.

Another problem you may be having is the :id parameter in your partial  
call–you might try taking it out and just referring to @post in your  
partial.

I’ve had problems even sending local variables to partials from render  
:update, so your best best is to just assign the variable in your  
controller and use it in the partial.

Jeff
