Search code examples
ruby-on-railsrubycomments

Comments Section Ordering The Comments Newest At Top Instead Of Bottom


I deployed a web app some weeks back where users make posts which others can comment on. The comments would order with oldest on the top and newest at the bottom. This worked fine until about 2 weeks ago I added the ability to edit posts and since then the comments and have been ordering the opposite way on all new posts. i.e. Newest at top.

I went through my commits and cant see what I could have done to cause the change. Worst of all it still works as I want it locally making it hard to debug.

Comment Controller:

class CommentsController < ApplicationController

  before_action :logged_in_user, only: [:create, :destroy]

  def create
    @micropost = Micropost.find_by(id: params[:micropost_id])
    @comment = 
   @micropost.comments.create(params[:comment].permit(:name,:body))
    @comment.name = current_user.name
    if @comment.save
      flash[:success] = "Comment Posted"
    end
    redirect_to request.referrer
 end

 def destroy
   @comment = Comment.find(params[:id])
   @comment.destroy
   redirect_to request.referrer
 end

end

The Show from the Microposts controller:

  def show
    @micropost = Micropost.find_by(id: params[:id])
    @post = Micropost.includes(:comments).find(params[:id])
    @micropost.punch(request)
    end

I tried to add .order(:created_at =>:desc) to the @post above but it made no difference. I made it :asc to test, but the comments wouldn't reorder. I have used the .order successfully in other places.

Comment View which is rendered on the Micropost view:

<li>
  <ol class="microposts">
    <% @post.comments.each do |comment| %>
      <li>
        <article class="article-container-full">
          <%=  comment.name %>
          <hr>
        <%= simple_format(comment.body) %>
          <hr>
          <p class="posted-time">
            Posted <%= time_ago_in_words(comment.created_at) %> ago
          </p>
        </article>
        <br>
       </li>
      </ol>
    <% end %>
</li>

Comment Form View

<%= form_for([@micropost, @micropost.comments.build]) do |f| %>
  <br>

  <h4 class="article-content">
    <%= f.hidden_field :name ,value: current_user.name %>
    <%= "Commenter: #{current_user.name}" %>
  </h4>

  <p>
    <%= f.text_area :body, rows:"4" ,required: true %>
  </p>

  <br>
   <p>
   <%= f.submit class: 'submit-button' %>
   </p>

  <% end %>

Micropost View Comment Render

 </li>
  <h1 class="article-container-header"> Comments</h1>
 <article>
   <%= render "comments/comment" %>
 </article>
 <% if logged_in? %>
  <article class="article-container-full">
   <%= render 'comments/form' %>
 </article>
 <% end %>

Ive tried using .order(:created_at =>:desc) or .order(:created_at =>:asc)in a few places but to no avail.

All suggestions welcome.


Solution

  • You need to order comments.

    in micropost view:

    <% @post.comments.order(created_at: :asc).each do |comment| %>
      ..
    <% end %>