3

I was working through the ruby on rails blogger tutorial on:
http://tutorials.jumpstartlab.com/projects/blogger.html#i2:-adding-comments
and i am at the point where I am fixing the show action for the blogger app.

here is my controller:

def show
@articles = Article.find(params[:id])
@comment = Comment.new
@comments = Comment.find(params[:id])
end


here is my show view - (which is rendering my comments partial)

`<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>
 `

here is my comments partial:

 <div>
 <% @comments.each do |comment| %>
 <h4>Comment by:<%=comment.author_name%></h4>
 <p class="comment"><%=comment.body%></p>
 <%end%>
 </div>

My problem is that when i am trying to display all the comments from the Comment model on the show page through the each iterator in the comments partial, I get this error:

 undefined method `each' for #<Comment:0x007f12ef55d0d0> 

I dont see any problems with my instance variables in the show action. and the each method that I wrote, has the proper syntax. Does anyone know What this error means?

1 Answer 1

3

Your error basically means that you are expecting an array of comments, but only have a comment. The way you called find will only return one object, not an array. You need to adjust the following line:

@comments = Comment.find(params[:id])

If your relationships are set up correctly, you should be able to replace it with something like this:

@comments = article.comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.