Forum: Ruby on Rails Ajax render a template

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.
John B. (Guest)
on 2006-04-07 17:29
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
<div id="divcomment">
<b>Leave a Reply</b>
<%= form_remote_tag(:update => 'newcomments', :url => {:action =>
:comment, :id => @post}, :position => "before") %>
<%= render :partial => 'commentform' %>
<p><%= submit_tag "Comment!" %></p>
<%= end_form_tag %>
</div>

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
<div id="newcomments">
    <div id="divcomment">
        <p><%= comment.name %> Says:</p>
        <p><%= comment.comment %></p>
        <p id="dateformat"><%= comment.created_at.to_s(:long) %></p>
        <hr/>
    </div>
</div>
Chris H. (Guest)
on 2006-04-07 18:33
(Received via mailing list)
using rjs, i'd do it something like so...

in your post view

<div id="post_<%= post.id -%>" class="post">
    ...your post here
  <div id="comments" class="comment_list">
    <%= render :partial => "comment", :collection => @comments %>
  </div>
  <div id="comment_form">
    <%= render :partial => "comment_form" %>
  </div>
</div>

_comment_form.rhtml

<!-- # submit form data via ajax and update the list of comments -->
<%= form_remote_tag(:url => {:action => :comment, :id => @post }) %>
<!-- form fields -->
<p><%= submit_tag "Comment!" %></p>
<%= end_form_tag %>

_comment.rhtml

<div id="comment_<%= comment.id -%>" class="comment">
  <p class="comment_author"><%= comment.name -%> Says:</p>
  <p class="comment_body"><%= comment.comment -%></p>
  <p class="comment_date"><%= comment.created_at.to_s(:long) -%></p>
  <hr/>
</div>

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
John B. (Guest)
on 2006-04-07 22:41
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?
Chris H. (Guest)
on 2006-04-07 23:18
(Received via mailing list)
rjs templates.

http://weblog.rubyonrails.org/articles/2006/03/28/...

read the first section and you'll get an taste of what rjs templates are
capable of.
This topic is locked and can not be replied to.