# Ajax render a template

**URL:** <https://www.ruby-forum.com/t/ajax-render-a-template/57150>\
**Category:** Rails\
**Created:** [April 7, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/ajax-render-a-template/57150 "2006-04-07T15:29:03Z")\
**Posts on this page:** 4\
**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 7, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/ajax-render-a-template/57150/1 "2006-04-07T15:29:03Z")

</div>

In my blog a user can add comments. I want an Ajax call to add the  
comment to the bottom of the comments list.

How to i return a comment thats is in the comment template and add it to  
the bottom of the page. I have gotten as far as rendering some text  
back to the form and the comment doesn’t display until i refresh.

This is my \_newcomment.rhtml form to create a new comment

**Leave a Reply** \<%= form\_remote\_tag(:update =\> 'newcomments', :url =\> {:action =\> :comment, :id =\> @post}, :position =\> "before") %\> \<%= render :partial =\> 'commentform' %\>

\<%= submit\_tag "Comment!" %\>

\<%= end\_form\_tag %\>

This is my def in the blog controller t handle the creation of the new  
comment  
def comment  
Post.find(params[:id]).comments.create(params[:comment])  
render\_text “return the comment”  
end

This is my partial to display a comment \_comment.rhtml

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

\<%= comment.comment %\>

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

* * *

---

<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 7, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/ajax-render-a-template/57150/2 "2006-04-07T16:33:30Z")

</div>

using rjs, i’d do it something like so…

in your post view

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

 \<%= render :partial =\> "comment\_form" %\> 

\_comment\_form.rhtml

\<%= form\_remote\_tag(:url =\> {:action =\> :comment, :id =\> @post }) %\>

\<%= submit\_tag "Comment!" %\>

\<%= end\_form\_tag %\>

\_comment.rhtml

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

\<%= comment.comment -%\>

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

* * *

controller:

def comment

# create comment

@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(“comment\_form”, :partial =\> “comment\_form”)  
end  
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 7, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/ajax-render-a-template/57150/3 "2006-04-07T20:41:48Z")

</div>

Chris H. wrote:

> using rjs, i’d do it something like so…

Using RJS?? Does this mean you have template file ending in RJS which  
adds a comment to the end of the list?

---

<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 7, 2006, 9:18pm UTC](https://www.ruby-forum.com/t/ajax-render-a-template/57150/4 "2006-04-07T21:18:21Z")

</div>

rjs templates.

[http://weblog.rubyonrails.org/articles/2006/03/28/rails-1-1-rjs-active-record-respond\_to-integration-tests-and-500-other-things](http://weblog.rubyonrails.org/articles/2006/03/28/rails-1-1-rjs-active-record-respond_to-integration-tests-and-500-other-things)

read the first section and you’ll get an taste of what rjs templates are  
capable of.
