title: Rails 3 -rendering partials and error "undefined local variable or method"
body: <p>I have a photos in gallery (<strong>PhotosController</strong>) and I wanna add to every photo a comments. Into the statement of photos I added a partial for rendering comments + form for adding new comment. </p>
  
  <p>My problem is, that when I send the form with a new comment, so I'll get the rendering error:</p>
  
  <pre><code>**NameError (undefined local variable or method `photo' for #&lt;CommentsController:0x00000100ce0ad0&gt;)**:
  </code></pre>
  
  <p>This is how looks my code:</p>
  
  <p><strong>views/photos/index.html.erb</strong></p>
  
  <pre><code>&lt;%= render @photos %&gt;
  </code></pre>
  
  <p><strong>views/photos/_photo.html.erb</strong></p>
  
  <pre><code>&lt;div&gt;&lt;%=image_tag photo.photo.url(:thumb)%&gt;&lt;/div&gt;
  &lt;div class="comments_to_photo"&gt;
    &lt;%= render :partial =&gt; 'photos/photo_comments', :locals =&gt; { :photo =&gt; photo }%&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>photos/photo_comments</strong></p>
  
  <pre><code>&lt;%photo.comments.each do |cmnt|%&gt;
    &lt;div&gt;&lt;%=cmnt.comment%&gt;&lt;/div&gt;
  &lt;%end%&gt; 
  
  &lt;%=form_tag comment_path, :remote =&gt; true do %&gt;
    &lt;div&gt;&lt;%=text_area_tag 'comment[comment]'%&gt;&lt;/div&gt;
    &lt;%=submit_tag%&gt;
  &lt;%end%&gt;
  </code></pre>
  
  <p><strong>controllers/CommentsController</strong></p>
  
  <pre><code>  def create
      @comment = Comment.new(params[:comment])
  
      respond_to do |format|
        if @comment.save
          format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
          format.js {
            render :partial =&gt; '/photos/photo_comments', :locals =&gt; { :photo =&gt; photo } do |page|; page &lt;&lt; "$(this).html('abcde');" end  
          }
        else
          format.html { render action: "new" }
          format.js
        end
      end
    end
  </code></pre>
  
  <p>I would like to refresh the form and comments statement in the photo, where was added a comment. Could anyone help me, please, how to avoid the error above?</p>
  
  <p>Thank you in advance</p>
  
  <p><strong>EDIT:</strong> I added for refresh comments through AJAX the file <strong>_photo_comments.js.erb</strong>:</p>
  
  <pre><code>$('#photo_comment').html("&lt;%= escape_javascript(render('photos/photo_comments')) %&gt;");
  </code></pre>
  
  <p>And I get the error</p>
  
  <pre><code>ActionView::Template::Error (stack level too deep):
    activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:23
  </code></pre>
  
  <p>with tens of this line:</p>
  
  <pre><code>  Rendered photos/_photo_comments.js.erb (562.2ms)
  </code></pre>
  
  <p>and the last one</p>
  
  <pre><code>Completed 500 Internal Server Error in 580ms
  </code></pre>
  
  <p>What's wrong with rendering? I can't render partial html file from partial JS file?</p>