2012-02-13 56 views
1

我在庫中有一張照片(PhotosController),我想添加到每張照片的評論。在照片聲明中,我添加了一部分用於呈現評論+表單以添加新評論。Rails 3 -rendering partials and error「undefined local variable or method」

我的問題是,當我用一個新的評論發送的形式,所以我會得到渲染錯誤:

**NameError (undefined local variable or method `photo' for #<CommentsController:0x00000100ce0ad0>)**: 

這是怎麼看我的代碼:

的意見/照片/index.html.erb

<%= render @photos %> 

的意見/照片/ _photo.html.erb

<div><%=image_tag photo.photo.url(:thumb)%></div> 
<div class="comments_to_photo"> 
    <%= render :partial => 'photos/photo_comments', :locals => { :photo => photo }%> 
</div> 

照片/ photo_comments

<%photo.comments.each do |cmnt|%> 
    <div><%=cmnt.comment%></div> 
<%end%> 

<%=form_tag comment_path, :remote => true do %> 
    <div><%=text_area_tag 'comment[comment]'%></div> 
    <%=submit_tag%> 
<%end%> 

控制器/ CommentsController

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 => '/photos/photo_comments', :locals => { :photo => photo } do |page|; page << "$(this).html('abcde');" end 
     } 
     else 
     format.html { render action: "new" } 
     format.js 
     end 
    end 
    end 

我想刷新照片,其中添加註釋的形式和意見陳述。任何人都可以幫助我,請如何避免上述錯誤?

預先感謝您

編輯:我添加了刷新意見通過AJAX文件_photo_comments.js.erb

$('#photo_comment').html("<%= escape_javascript(render('photos/photo_comments')) %>"); 

而我得到的錯誤

ActionView::Template::Error (stack level too deep): 
    activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:23 

有幾十條此線:

Rendered photos/_photo_comments.js.erb (562.2ms) 

,最後一個

Completed 500 Internal Server Error in 580ms 

有什麼不好的渲染?我不能從部分JS文件呈現部分HTML文件?

回答

3

在你的控制器,你有:

:locals => { :photo => photo } 

但是變量photo不存在那裏。它應該是:

:locals => { :photo => @comment.photo } 
+0

謝謝,這解決了我的問題。我試圖通過AJAX渲染評論聲明,但我得到了錯誤「堆棧層次太深」 - 你有什麼想法,我可以忘記嗎? (我更新了我的問題)。謝謝 – user984621 2012-02-13 15:24:22

0

image_tag photo.photo.url(:thumb) < - 是你想要的嗎photo.photo

+0

是的,因爲我有表**照片**的**列** photo_file_name,** ** photo_content_type - 這是正確的,圖像右側顯示,但是當我想要通過AJAX呈現partials,我得到上面的錯誤... – user984621 2012-02-13 13:57:52

相關問題