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 <div> 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' %>
<h2>Comments</h2>
<div id='comment_list'> <% for comment in @post.comments %> <%= comment.body %> <hr /> <% end %> </div>
<% 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" %> <br /> <%= 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?