开发者

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>
[#&lt;Comment id: 97, name: &quot;hmmm?&quot;, body: &quot;hmmm&quot;, 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| %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜