I'm building a simple Blog application using Rails and I'm trying to create the ability to submit a comment for a post. However, I keep getting this error
First argument in form cannot contain nil or be empty
<h3> Submit a Comment </h3>
<%= form_for [@post, @comment] do |c| %>
//Error is here, @comment appears to be nil and I'm not sure why
//(I've checked both cases)
<%= c.label :body, "Comment: " %>
<%= c.text_area :body %>
class CommentsController < ApplicationController
@post = Post.find params[:post_id]
comment_params = params.require(:comment).permit(:body)
@comment = Comment.new comment_params
@comment.post = @post
# Why is my comment nil?
redirect_to post_path(@post), notice: "Comment successful"
flash[:alert] = "Comment unsuccessful. Please do not enter an empty comment"
# To be implemented
You're creating a new comment. You should either:
@comment = Comment.new somewhere in your controller
B. Instantiate it right then and there with
form_for [@post, Comment.new] do |c| #...