views:

35

answers:

1

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!

+1  A: 

You should use:

render :partial => @comment

or

render :partial => "comments/comment", :object => @comment

or

render :partial => "comments/comment", :locals => {:comment => @comment}
PeterWong
Why the 3 options?
AnApprentice
Just tried all three which each errored is the syntax correct? example "$(".cmtBox").append("<%= escape_javascript(render :partial => "comments/comment", :locals => {:comment => @comment}) %>");"
AnApprentice
Is your _comment partial located in the /app/views/comments directory?
Yannis
Ya it's /app/views/comments/_comment.html.erb
AnApprentice
Your comment's syntax seems correct. Is there any error messages? Still nil? BTW, you used `$(".cmtBox")`, which is the general class for every comment boxes, you will be appending the new comment to every existing comment boxes! Use `$(".cmtBox").last()` instead.
PeterWong
Is there a good way to debug this with Rails? Perhaps the controller isn't sending or making avaialble the @comment?
AnApprentice
I think it must be that "@comment does not exists inside the create action" How can I add it? given the above?
AnApprentice
try using `debug @comment` to output html format of `@comment` (similar to `"<pre>" + @comment.inspect + "</pre>"` in your view files.
PeterWong