refer to https://legacy.gitbook.com/book/spartchou/ruby-on-rails-basic/details
Do not use @post.user_id = current_user.id
, and use save!
instead of save
unless check return value of save
class PostsController < ApplicationController
# @post.user_id = current_user.id and
def create
@post = current_user.posts.build(params[:post])
@post.save!