Reputation: 605
All,
I am getting the error: Validation failed: User can't be blank. I am getting this error when I save down a comment. Can anyone point me in the right direction possibly?
The comments controller
class CommentsController < ApplicationController
def create
@topic = Topic.find(params[:topic_id])
@post = @topic.posts.find(params[:post_id])
@comment = @post.comments.new(params.require(:comment).permit(:body))
@comment.save!#save the code down in the database
redirect_to [@topic, @post]
end
end
Comment model
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
validates :body, length: { minimum: 5 }, presence: true
validates :user_id, presence: true
end
Error on page when saving down to the database
Upvotes: 0
Views: 5306
Reputation: 5206
You are validating the presence of the user in the comment, but the user is not been assigned to the comment.
Before saving the model, try:
@comment.user = current_user
Upvotes: 2