jeudi 29 avril 2021

to show first the comment left by the current_user using ORDER BY CASE

Into the pages where I show the comments I noted that the previous developer used this system (very good and it works perfectly)

in comments_controller.rb there is this

  def index
    @comments = movie.comments.roots.latest
                    .includes(:user).page(params[:page])
  end

in views/comments/index.html.erb there is this

The Comments
<div id="commentsContainer">
    <%= render partial: 'comment', collection: @comments %>
    <%= paginate @comments %>
</div>

and in view/comments/comment.html.erb there is this

<div id=<%= dom_id(comment) %>>
    <!-- Author -->
    <%= link_to comment.username, user_path(comment.username) %>
    <!-- Body Comment -->
    <%== parse_markdown(comment.text) %>
</div>

Now are showing the latest comments first.

I have to edit it to show first the comment left by the user (logged) that is watching the page and then the latest comments

So I should check if there is a comment left by the current_user and then to show it first. Should I use order by case? how to do?

Aucun commentaire:

Enregistrer un commentaire