Answer the question
In order to leave comments, you need to log in
ActiveRecord::RecordInvalid in CommentsController#create. How to transfer a user?
I'm trying to assign a user but rails complains ActiveRecord::RecordInvalid in CommentsController#create Errors occurred: User cannot be empty
# Not implemented: check to see whether the user has permission to create a comment on this object
@comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
if @comment.save!
render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
else
redirect_to root_path
class CommentsController < ApplicationController
def create
@comment_hash = params[:comment]
@obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
@comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
if @comment.save!
render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
else
redirect_to root_path
end
end
end
Answer the question
In order to leave comments, you need to log in
@comment = Comment.build_from(@obj, current_user, @comment_hash[:body])
there are two options, either curent_user returns an object of class not an Active Record, or generally nil if you send an Ajax request and do not write
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question