userがpostとcommentをhas_manyで持っていて、postがcommentをhas_manyで持っている状態です。
以下はcommentコントローラーのcreateアクションなのですが、ここで
@comment.user_id = current_user.idと@comment.post_id = @post.idを明記しているのは、@comment.saveする時に、commentテーブルのカラム全てが埋まっている必要があるからだと思うのですが、そういった解釈でいいでしょうか?
comment.controller
1before_action :authenticate_user! 2def create 3 @post = Post.find(params[:post_id]) 4 @comment = Comment.create(params[:comment].permit(:content)) 5 @comment.user_id = current_user.id ←これ 6 @comment.post_id = @post.id ←これ 7 8 if @comment.save 9 redirect_to post_path(@post) 10 else 11 render "new" 12end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/17 00:16