且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向 User 和 Post 模型添加评论(Ruby on Rails)

更新时间:2023-12-02 21:35:04

你应该按照自己的方式来:

In your way you should put this:

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.create(comment_params)
  @comment.user_id = current_user.id #or whatever is you session name
  if @comment.save
    redirect_to @post
  else
    flash.now[:danger] = "error"
  end
end

并且您还应该从 comment_params 中删除 user_id 作为强参数.希望这对你有帮助.

And also you should remove user_id from comment_params as strong parameters . Hope this will help you .