Add Comment to User and Post models (Ruby on Rails)

前端 未结 3 927
悲哀的现实
悲哀的现实 2021-02-01 10:30

I\'m new to Rails. I\'m building my first app - simple blog. I have User and Post models, where each user can write many posts. Now I want to add Comment model, where each post

3条回答
  •  深忆病人
    2021-02-01 11:09

    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
    

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

提交回复
热议问题