我正在学习 ruby on rails 博主教程:
http://tutorials.jumpstartlab.com/projects/blogger.html#i2:-adding-comments
我正处于修复博客应用程序的显示操作的地步。
这是我的 Controller :
def show
@articles = Article.find(params[:id])
@comment = Comment.new
@comments = Comment.find(params[:id])
end
这是我的展示 View -(呈现我的部分评论)
`<br/>
<br/>
<br/>
<%=render partial: 'comments/form'%>
<br/>
<h3>Comments</h3>
<%= render partial: 'articles/comment'%>
<br/>
<br/>
<br/>
<small><%= link_to "<< Back to Articles List", articles_path %></small>
`
这是我的部分评论:
<div>
<% @comments.each do |comment| %>
<h4>Comment by:<%=comment.author_name%></h4>
<p class="comment"><%=comment.body%></p>
<%end%>
</div>
我的问题是,当我试图显示 Comment 模型中的所有评论时 在通过评论部分中的每个迭代器显示页面上,我收到此错误:
undefined method `each' for #<Comment:0x007f12ef55d0d0>
我在 show Action 中没有看到我的实例变量有任何问题。和每个方法 我写的,有正确的语法。有谁知道这个错误是什么意思?
最佳答案
您的错误基本上意味着您期待一系列评论,但只有一条评论。您调用 find 的方式只会返回一个对象,而不是数组。您需要调整以下行:
@comments = Comment.find(params[:id])
如果你的关系设置正确,你应该能够用这样的东西替换它:
@comments = article.comments
关于ruby-on-rails - #<Comment :0x007f12ef55d0d0> 的未定义方法 `each',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976730/