Rails 3 - 渲染部分和错误“未定义的局部变量或方法”

发布于 2025-01-05 01:20:56 字数 2255 浏览 2 评论 0原文

我在画廊(PhotosController)中有一张照片,我想为每张照片添加评论。在照片声明中,我添加了用于渲染评论的部分+用于添加新评论的表单。

我的问题是,当我发送带有新评论的表单时,我会收到渲染错误:

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

这就是我的代码的样子:

views/photos/index.html.erb

<%= render @photos %>

views/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>

photos/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

  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 文件?

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.

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

<%= render @photos %>

views/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>

photos/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

  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?

Thank you in advance

EDIT: I added for refresh comments through AJAX the file _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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2025-01-12 01:20:56

在你的控制器中,你有:

:locals => { :photo => photo }

但是变量photo在那里不存在。应该是:

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

In your controller you have:

:locals => { :photo => photo }

but the variable photo doesn't exist there. It should be:

:locals => { :photo => @comment.photo }
九局 2025-01-12 01:20:56

image_tag photo.photo.url(:thumb) <- 这是您想要的 photo.photo 吗?

image_tag photo.photo.url(:thumb) <- Is it what you want to have photo.photo ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文