我正在尝试向帖子模型添加评论。到目前为止,这是我的 comments_controller:
class CommentsController < ApplicationController
before_action :find_post
def index
@comments = @post.comments.order('id')
end
def new
@comment = @post.comments.new
end
def create
@comment = @post.comments.build(params[:comment])
@comment.user = current_user
@comment.save
redirect_to post_path(@post)
end
private
def comment_params
params.require(:comment).permit(:comment_body)
end
def find_post
@user = current_user
@post = @user.posts.find(params[:post_id])
end
end
我收到此错误:
CommentsController 中的 NoMethodError #new undefined method `posts' for nil:NilClass
如您所见,我是编程新手。感谢帮助!:)