I am working a blog system for my girlfriend, and am trying to add
AJAX to many places throughout the blog where refreshing is most
troublesome; like when visitors post comments!
Basically I have a text area for the comment and a submit button, what
I want to do is obviously only refresh the
which contains the
current comments when a user submits their comment.
I thought I had figured it out, however when a user posts a comment
the comment_list div shows empty until the page is refreshed 
Here is the add_comment action in my controller:
def add_comment
Post.find(params[:id]).comments.create(params[:comment])
end
And here is my show.rhtml which shows the blog post + comments and
comment form :
<%= javascript_include_tag “prototype” %>
<%= render :partial => “post”, :object => @post %>
<%= link_to ‘Edit’, :action => ‘edit’, :id => @post %> |
<%= link_to ‘Back’, :action => ‘list’ %>
Comments
<% for comment in @post.comments %>
<%= comment.body %>
<% end %>
<% form_remote_tag :url => { :action => “add_comment”, :id =>
@post }, :html => {:name => ‘add_comment_form’}, :update =>
‘comment_list’, :success => visual_effect(:highlight, “comment_list”)
do %>
<%= text_area “comment”, “body” %>
<%= submit_tag “Comment!” %>
<% end %>
Also I had to create a blank add_comment.rhtml view otherwise I kept
getting template errors.
Anyone got any suggestions/ideas?
Hi,
If I understand issue correctly, the add_comment isn’t rendering
anything hence comment_list isn’t updated.
Try this:
Move the <% for comment in @post.comments %>…<% end %> loop into a
partial called _post_comments_list.rhtml. Replace the loop in
show.rhtml with:
<%= render :partial=>'post_comments_list'
This calls the partial from show.
Replace add_comment with:
def add_comment
@post = Post.find(params[:id])
@post.comments.create(params[:comment])
render :partial=>‘post_comments_list’
end
This sets the @post variable, creates the comments, and renders the
comments list for the @post variable.
The above isn’t tested but I hope it does what you want.
Hope this helps!
Shawn
removed_email_address@domain.invalid wrote:
I am working a blog system for my girlfriend, and am trying to add
AJAX to many places throughout the blog where refreshing is most
troublesome; like when visitors post comments!
Basically I have a text area for the comment and a submit button, what
I want to do is obviously only refresh the
which contains the
current comments when a user submits their comment.
I thought I had figured it out, however when a user posts a comment
the comment_list div shows empty until the page is refreshed 
Here is the add_comment action in my controller:
def add_comment
Post.find(params[:id]).comments.create(params[:comment])
end
And here is my show.rhtml which shows the blog post + comments and
comment form :
<%= javascript_include_tag “prototype” %>
<%= render :partial => “post”, :object => @post %>
<%= link_to ‘Edit’, :action => ‘edit’, :id => @post %> |
<%= link_to ‘Back’, :action => ‘list’ %>
Comments
<% for comment in @post.comments %>
<%= comment.body %>
<% end %>
<% form_remote_tag :url => { :action => “add_comment”, :id =>
@post }, :html => {:name => ‘add_comment_form’}, :update =>
‘comment_list’, :success => visual_effect(:highlight, “comment_list”)
do %>
<%= text_area “comment”, “body” %>
<%= submit_tag “Comment!” %>
<% end %>
Also I had to create a blank add_comment.rhtml view otherwise I kept
getting template errors.
Anyone got any suggestions/ideas?