denvar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-25 21:31 UTC
Adding AJAX ''Post a Comment'' to blog
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: <div id=''comment_list''> <%= render :partial=>''post_comments_list'' </div> 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 denvar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 <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?-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
denvar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-26 14:57 UTC
Re: Adding AJAX ''Post a Comment'' to blog
Worked a treat thanks! On Mar 26, 5:37 am, Shawn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: > > <div id=''comment_list''> > <%= render :partial=>''post_comments_list'' > </div> > > 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 > > > > den...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 <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? > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---