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!

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

@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

@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 :slight_smile:

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

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().

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?

if you want to just pull the current comment do it like sunny said

Thanks but that didn't work. Can you show me an example of the
controller update to get this to work? Thxs

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

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!