我正在浏览ruby on rails blogger教程:
http://tutorials.jumpstartlab.com/projects/blogger.html#i2:-adding-comments
而且我正在修复博客应用程序的show动作。
这是我的控制器:
def show
@articles = Article.find(params[:id])
@comment = Comment.new
@comments = Comment.find(params[:id])
end
这是我的节目视图 - (这是我的评论部分)
`<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>
我的问题是,当我试图显示评论模型中的所有评论时 在显示页面上通过评论部分中的每个迭代器,我收到此错误:
undefined method `each' for #<Comment:0x007f12ef55d0d0>
我在show动作中没有看到我的实例变量有任何问题。和每种方法 我写的,有正确的语法。有谁知道这个错误意味着什么?
答案 0 :(得分:3)
您的错误基本上意味着您期待一系列评论,但只有评论。您调用find的方式只返回一个对象,而不是数组。您需要调整以下行:
@comments = Comment.find(params[:id])
如果您的关系设置正确,您应该能够用以下内容替换它:
@comments = article.comments