nobosh
2010-Sep-27 06:50 UTC
Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
Here''s the flow I have... First, jquery posts the new comment to the server: $.post(this.action,$(this).serialize(),null,''script''); Then in the comments controller: def create @comment = lots of stuff going on here but it works... if @comment.save flash[:notice] = "Successfully created comment." respond_to do |format| format.js end end Ok and this is where I''m stuck, then the create.js.erb: $(".cmtBox").html("<%=escape_javascript(render :partial =>"comments/comment")%>"); And the partial: <div class="cmtBox" id="comment_<%=comment.id%>"> <%=comment.content%> </div> Where I''m stuck is calling the partial in create.js.erb... How do I pass what Rails needs to populate the partial? Right now I get the error: "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id" Thanks! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sunny Ezror
2010-Sep-27 09:10 UTC
Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
Have you tried passing in locals for the partial? Not sure how Rails 3 does it, but Rails 2 its <%=escape_javascript(render :partial =>"comments/comment", :locals => {:comment => @comment))%> -sunny http://ezror.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Sep-27 12:45 UTC
Re: Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
@sunny @comment does not exists inside the create action, and he needs to iterate over a collection , @comments <%=escape_javascript(render :partial =>"comments/comment", :locals => {:comments =>fill the comments collection...))%> or @comment = lots of stuff going on here but it works... @comments = fill the comments collection.. if @comment.save flash[:notice] = "Successfully created comment." respond_to do |format| format.js end end then <%=escape_javascript(render :partial =>"comments/comment", :locals => {:comments => @comments))%> @comments does not exists inside the response from create.js.erb so you have to build it there or in the controller''s create action before the respond_to block you can check my example http://github.com/rbritom/Simple_polymorphic_nested_comments -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Sep-27 14:01 UTC
Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
@radhames thanks but in create.js.erb I do not want comments, just the @comment that was just created via a AJAX post with the comment controller''s create... I''m using your comments code example in the link you listed, it''s great :) But your example in create.js.erb sends back ALL the comments for a model, I just want to return the comment that was just created. Can you show me how to do that in your example? What should craete.js.eb look like? Thanks On Sep 27, 5:45 am, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @sunny @comment does not exists inside the create action, and he needs to > iterate over a collection , @comments > > <%=escape_javascript(render :partial =>"comments/comment", :locals => > {:comments =>fill the comments collection...))%> > > or > > @comment = lots of stuff going on here but it works... > @comments = fill the comments collection.. > if @comment.save > flash[:notice] = "Successfully created comment." > respond_to do |format| > format.js > end > end > > then > > <%=escape_javascript(render :partial =>"comments/comment", :locals => > {:comments => @comments))%> > > @comments does not exists inside the response from create.js.erb so you have > to build it there or in the controller''s create action before the respond_to > block > > you can check my example > > http://github.com/rbritom/Simple_polymorphic_nested_comments-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Sep-27 14:15 UTC
Re: Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
sure, just do it like sunny said, you are only going to display one comment if you user $(".cmtBox").html() if want to add one to the top use insertBefore(). -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Sep-27 21:06 UTC
Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
Thanks. The JQuery is easy the problem is the partial in your sample app displays a list of several comments were I want to send back to the user just the recently submitted comment. Thoughts? On Sep 27, 7:15 am, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> sure, just do it like sunny said, you are only going to display one comment > if you user $(".cmtBox").html() if want to add one to the top use > insertBefore().-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Sep-27 21:57 UTC
Re: Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
if you want to just pull the current comment do it like sunny said -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Sep-27 22:24 UTC
Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
Thanks but that didn''t work. Can you show me an example of the controller update to get this to work? Thxs On Sep 27, 2:57 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> if you want to just pull the current comment do it like sunny said-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sunny Ezror
2010-Sep-27 22:45 UTC
Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
What error are you getting? nobosh wrote:> Thanks but that didn''t work. Can you show me an example of the > controller update to get this to work? Thxs-sunny http://ezror.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Sep-28 00:41 UTC
Re: Rails 3 - Creating a comment and then returning the Partial with JUST the new comment
Got it working by deleting everything and starting from scratch... Not sure what was the problem but using the above helped me get it right from scratch. Thanks everyone! On Sep 27, 3:45 pm, Sunny Ezror <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> What error are you getting? > > nobosh wrote: > > Thanks but that didn''t work. Can you show me an example of the > > controller update to get this to work? Thxs > > -sunnyhttp://ezror.com > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.