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
On 4/7/06, John Butler <JohnnyButler7@gmail.com>
wrote:>
> 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>
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060407/cd5771c8/attachment.html