I have a photos in gallery (PhotosController) 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.
我在图片库(PhotosController)有一张照片,我想给每张照片添加一个评论。在photos语句中,我添加了一个用于呈现注释的部分+用于添加新注释的表单。
My problem is, that when I send the form with a new comment, so I'll get the rendering error:
我的问题是,当我用新的注释发送表单时,我将得到呈现错误:
**NameError (undefined local variable or method `photo' for #<CommentsController:0x00000100ce0ad0>)**:
This is how looks my code:
我的代码是这样的:
views/photos/index.html.erb
视图/照片/ index.html.erb
<%= render @photos %>
views/photos/_photo.html.erb
视图/照片/ _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>
photos/photo_comments
照片/ 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%>
controllers/CommentsController
控制器/ 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
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?
我想刷新一下照片中的表单和comments语句,其中添加了注释。请问有谁可以帮助我,如何避免上面的错误?
Thank you in advance
提前谢谢你
EDIT: I added for refresh comments through AJAX the file _photo_comments.js.erb:
编辑:我通过AJAX添加了文件_photo_comments.js.erb:
$('#photo_comment').html("<%= escape_javascript(render('photos/photo_comments')) %>");
And I get the error
得到误差
ActionView::Template::Error (stack level too deep):
activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:23
with tens of this line:
有几十条这样的线:
Rendered photos/_photo_comments.js.erb (562.2ms)
and the last one
最后一个
Completed 500 Internal Server Error in 580ms
What's wrong with rendering? I can't render partial html file from partial JS file?
呈现怎么了?我不能从部分JS文件渲染部分html文件?
2 个解决方案
#1
3
In your controller you have:
在控制器中你有:
:locals => { :photo => photo }
but the variable photo
doesn't exist there. It should be:
但是变量photo并不存在。应该是:
:locals => { :photo => @comment.photo }
#2
0
image_tag photo.photo.url(:thumb)
<- Is it what you want to have photo.photo
?
image_tag photo.photo.url(:thumb) <-这是你想要的照片吗?照片吗?
#1
3
In your controller you have:
在控制器中你有:
:locals => { :photo => photo }
but the variable photo
doesn't exist there. It should be:
但是变量photo并不存在。应该是:
:locals => { :photo => @comment.photo }
#2
0
image_tag photo.photo.url(:thumb)
<- Is it what you want to have photo.photo
?
image_tag photo.photo.url(:thumb) <-这是你想要的照片吗?照片吗?