How do I render all Comments in a Rails view?

blogs, comments, ruby, ruby-on-rails

Solution

If you want to display all the comments from any post, in most recent order, you could do:

@comments = Comment.find(:all, :order => 'created_at DESC', :limit => 10)

And in the view you can do:

<% @comments.each do |comment| -%>
    <p>
        <%= comment.text %> on the post <%= comment.post.title %>
    </p>
<% end -%>

Problem

I am new to rails so go easy. I have created a blog. I have successfully implemented comments and attached them to each post. Now...I would like to display, in the sidebar, a list of the most recent comments from across all posts. I think there are two things involved here, an update to the comment_controller.rb, and then the call from the actual page. Here is the comments controller code. ``` class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create!(params[:comment]) respond_to do |format| format.html { redirect_to @post} format.js end end end ```

Original source