投稿モデルにコメントを追加しようとしています。これまでのところ、これは私の 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#nil:NilClass の新しい未定義メソッド「posts」
ご覧のとおり、私はプログラミングが初めてです。手伝ってくれてありがとう!:)