Rails 3 - Why is my Post#Show view showing all of its comments' database data in one big block after the comments list?
In a Rails 3 blog type of app, I have a polymorphic comments model, with Posts having Comments (through :commentable). The Post#Show page has a form for comments that post through AJAX to a comment list below. Everything is working fine EXCEPT that Rails is for some reason dumping a full list of the database entries for all comments on that post in []'s at the very bottom of my comments list. I can't figure out where this is coming from and why!
Here are maybe the relevant code chunks, please feel free to request more! Thanks in advance.
views/posts/show
...
<%= render 'comments' %>
...
views/posts/_comments
<%= render :partial => 'comments/form' %>
<ul class="comments">
<% if @post.comments.empty? %>
<li>none yet</li>
<% else %>
<%= render :partial => 'comments/comment' %>
<% end %>
</ul>
views/comments/comment
<%= @comments.each do |comment| %>
<li>
<p class="comment_body">"<%= comment.body %>"</p>
<br/>
<p class="comment_info"><%= comment.name %> - <%= time_ago_in_words(comment.created_at) %> ago </p>
</li>
<% end %>
posts controller
def show
@post = Post.find(params[:id])
@comments = @post.comments
@commentable = @post
@comment = Comment.new(:commentable => @post)
@title = @post.author
end
And here's where the error creeps in (from Page Source). Right after the last comment closes and before the comments closes:
....
</li>
[#<Comment id: 97, name: "hmmm?", body: "hmmm", created_a...
</ul>
开发者_运维技巧
What is that thing and why is it here!? Thanks.
it's the =
in the @comments.each
tag. It is returning the result of each
, which is the whole array.
For example:
irb> [1,2].each {|i| puts i }
1
2
=> [1, 2]
So:
<%= @comments.each do |comment| %>
Should simply be:
<% @comments.each do |comment| %>
精彩评论