I have this part of code:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
http://guides.rubyonrails.org/getting_started.html#generating-a-controller
But I have one more column “user_id” in my comment table. How can I assotiate user_id with current_user.id?
ps. I have working example in my app:
@post = Post.new(params[:post])
@post.user_id = current_user.id
I would say something like this.
you could put the user_id in the params but that wouldnt be safe. user_id shouldnt be in ‘attr_accessable’ so it will be protected for mass_assignment.
You could do the above in a one liner but personally i dont like that. I think like this, it is more clear. If you want to have error handling you could do the save like this.