Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying most recent comments in my def show action
    text
    copied!<p>I am having a bit of trouble displaying the most recent created comments based from the time/date that they were created in my <code>views/post/show.htmlerb</code> file. I just got my <code>posts_controller</code> to display the most recent created posts from the <code>def index action</code> but now in my <code>def show</code> action the following code doesn't work:</p> <p><code>@comment_date_order = Comment.find(params[:id]).comments.order('created_at DESC')</code></p> <p>this is my full posts_controller.rb file:</p> <pre><code>class PostsController &lt; ApplicationController before_action :set_post, only: [:show, :edit, :update, :vote] before_action :require_user, only: [:new, :create, :edit, :update, :vote] before_action :require_creator, only:[:edit, :update] def index @posts = Post.page(params[:page]).order('created_at DESC').per_page(10) end def show @comment = Comment.new @comment_date_order = Post.find(params[:id]).comments.order('created_at DESC') end def new @post = Post.new end def create @post = Post.new(post_params) @post.creator = current_user if @post.save flash[:notice] = "You created a post!" redirect_to posts_path else render :new end end def edit end def update @post = Post.find(params[:id]) if @post.update(post_params) flash[:notice] = "You updated the post!" redirect_to post_path(@post) else render :edit end end def vote Vote.create(voteable: @post, creator: current_user, vote: params[:vote]) respond_to do |format| format.js { render :vote } # Renders views/posts/vote.js.erb end end private def post_params params.require(:post).permit(:url, :title, :description) end def set_post @post = Post.find(params[:id]) end def require_creator access_denied if @post.creator != current_user end end </code></pre> <p>comments_controller.erb file:</p> <pre><code>class CommentsController &lt; ApplicationController before_action :require_user def create @post = Post.find(params[:post_id]) @comment = Comment.new(params.require(:comment).permit(:body)) @comment.post = @post @comment.creator = current_user if @comment.save flash[:notice] = "Your comment was created!" redirect_to post_path(@post) else render 'posts/show' end end def edit @comment = Comment.find(params[:id]) @post = Post.find(params[:post_id]) end def update @comment = Comment.find(params[:id]) if @comment.update(comment_params) flash[:notice] = "You updated your comment!" redirect_to post_path(@post) else render :edit end end private def comment_params params.require(:comment).permit(:body) end def set_comment @comment = Comment.find(params[:id]) end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload